我使用的是rails 4.0.5,rspec 2.14.1,capybara 2.2.1,capybara-webkit 1.1.0和database_cleaner 1.2.0.我通过以下功能测试看到一些奇怪的行为(模拟用户在帖子上查看评论,将鼠标悬停在图标上以显示菜单,然后单击菜单项以删除评论):
let(:user){create(:user)}
let(:post){create(:post, author: user)}
let!(:comment){create(:comment, post: post, author: user)}
...
it "can delete a comment" do
assert(page.has_css? "#comment-#{comment.id}")
find("#comment-#{comment.id}-controls").trigger(:mouseover)
find("#comment-#{comment.id} .comment-delete a").click
assert(page.has_no_css? "#comment-#{comment.id}")
end
Run Code Online (Sandbox Code Playgroud)
这个测试大约80%的时间都失败了,总是由于从数据库中检索到一些记录nil- 我得到的NoMethodError: undefined method X for nil:NilClass,对于X的各种值.有时nil是被删除的注释,有时它是注释附加的帖子到,有时它是评论/帖子的作者.
如果我添加sleep 1到测试结束,它会通过:
it "can delete its own comment" do
assert(page.has_css? "#comment-#{comment.id}")
find("#comment-#{comment.id}-controls").trigger(:mouseover)
find("#comment-#{comment.id} .comment-delete a").click
assert(page.has_no_css? "#comment-#{comment.id}")
sleep 1
end
Run Code Online (Sandbox Code Playgroud)
如果我放入sleep 1一个after区块,它也会通过.
知道为什么我得到这些NoMethodErrors,和/或为什么测试通过,如果我在完成所有工作后让它睡了一秒钟?
当我运行这个RSpec示例时,它会通过,但我收到了弃用警告.
context "should have valid detail for signup" do
it "should give error for invalid confirm password" do
find("#usernamesignup").set("Any_name")
find("#mobile_number").set("1234567890")
find("#emailsignup").set("mymail@yopmail.com")
find("#passwordsignup").set("12345678")
find("#passwordsignup_confirm").set("")
find(".signin").click
sleep 2
page.should have_content "Confirm Password Does not Match"
end
end
Run Code Online (Sandbox Code Playgroud)
这是输出:
弃用警告:
不推荐使用
shouldrspec-expectations的旧:should语法而不显式启用语法.使用新的:expect语法或明确启用:should与config.expect_with(:rspec) { |c| c.syntax = :should }替代.来自/home/rails/rails_nimish/Devise_use/spec/features/users_spec.rb:113:in`block(4 levels)in'
如何解决此警告?
更新: 我刚刚更换的解决方案
page.should have_content"确认密码不匹配"
有:
expect(page).to have_content "Confirm Password Does not Match"
Run Code Online (Sandbox Code Playgroud)