1

使用 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
4

2 回答 2

0

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

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

于 2013-10-29T13:23:55.173 回答
-1

Likely problem is that post[title] and post[body] are the names of the fields, and not the IDs.

Also, something you might want to look into to make your tests a little more rigorous: capybara has a builtin within function that yields a block in which you can perform more actions. Check out the documentation on the front page of the gem page: https://github.com/jnicklas/capybara. It would probably look something like:

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
于 2013-10-29T13:51:38.087 回答