【问题标题】:capybara+rspec erase form text fieldscapybara+rspec 擦除表单文本字段
【发布时间】:2013-10-29 22:37:19
【问题描述】:

使用 capybara+rspec 如何编译带有空字段的表单? 我正在测试一个编辑资源页面,所以我有一个已编译的表单并想要清理它的文本字段。这是部分测试:

context "when submitting" do
        before { visit edit_post_path(post) }
        it {should have_content('Editing')}
        it {current_path.should == edit_post_path(post)}

        describe "whit invalid information" do
          before do
            fill_in "post[title]",    :with => "" #not working
            fill_in "post[body]", :with => "" #not working
            click_button "update"
          end
          it {current_path.should == edit_post_path(post)}
        end

        describe "whit valid information" do
          before do
            fill_in "post[title]",    with: "some"
            fill_in "post[body]", with: "some"
            click_button "update"
          end
          it {should have_content('some')}
          it {should have_content('some')}
          it {current_path.should == post_path(post)}

        end
end

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara


    【解决方案1】:

    通过 Chrome:InspectElement of Firefox:Firebug 在编辑页面生成的 HTML 中手动检查相关字段的实际 ID/名称/标签。很有可能它们与“post[title]”不同。

    UPD。尝试在页面上手动填写空字符串。它工作正常吗?我的意思是显示错误并且路线正确。触发“更新”按钮并收到错误消息,您不再使用edit_post_path。如果@post.update 不成功,您正在从Post#update 操作渲染Post#edit-view,则会发生这种情况。

    【讨论】:

    • 描述“whit valid information”有效,所以post[title]和post[body]都可以。
    【解决方案2】:

    可能的问题是 post[title]post[body] 是字段的名称,而不是 ID。

    此外,您可能需要考虑一些事情以使您的测试更加严格:capybara 有一个内置的within 函数,它产生一个块,您可以在其中执行更多操作。查看 gem 页面首页的文档:https://github.com/jnicklas/capybara。它可能看起来像:

    describe "whit invalid information" do
      before do
        within("#post") do
          fill_in "title", :with => ""
          fill_in "body", :with => ""
          click_button "update"
        end
      end
      it {current_path.should == edit_post_path(post)}
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多