我正在尝试测试登录时应该/不应该看到的链接,但RSpec会抛出以下错误:
Failure/Error: before { sign_in user }
ThreadError:
Attempt to unlock a mutex which is not locked
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:5:in `sign_in'
# ./spec/views/header_spec.rb:19:in `block (3 levels) in <top (required)>'
我的测试块看起来像这样:
describe "when signed in" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
it { should have_link("Sign out") }
it { should_not have_link("Sign up") }
it { should_not have_link("Log in") }
end
我的sign_in功能:
def sign_in(user)
click_link "Log in"
fill_in "username", with: user.username
fill_in "password", with: user.password
click_button "Log me in!"
cookies[:remember_token] = user.remember_token
end
我尝试过更换
click_button "Log me in!"
与
find(:button, "Log me in!").click
并导致以下错误:
Nokogiri::CSS::SyntaxError:
unexpected '!' after '[#<Nokogiri::CSS::Node:0x00000003645778 @type=:DESCENDANT_SELECTOR, @value=[#<Nokogiri::CSS::Node:0x000000036464e8 @type=:ELEMENT_NAME, @value=["Log"]>, #<Nokogiri::CSS::Node:0x000000036458b8 @type=:DESCENDANT_SELECTOR, @value=[#<Nokogiri::CSS::Node:0x00000003645f98 @type=:ELEMENT_NAME, @value=["me"]>, #<Nokogiri::CSS::Node:0x000000036459f8 @type=:ELEMENT_NAME, @value=["in"]>]>]>]'
# (eval):3:in `_racc_do_parse_c'
# (eval):3:in `do_parse'
# (eval):2:in `find'
# ./spec/support/utilities.rb:5:in `sign_in'
# ./spec/views/header_spec.rb:19:in `block (3 levels) in <top (required)>'
然后我尝试用“登录我”替换我的按钮文本,没有感叹号, 和Capybara抛出一个ElementNotFound错误:
Failure/Error: before { sign_in user }
Capybara::ElementNotFound:
Unable to find css "Log me in"
# (eval):2:in `find'
# ./spec/support/utilities.rb:5:in `sign_in'
# ./spec/views/header_spec.rb:19:in `block (3 levels) in <top (required)>'
我对RSpec和Rails一般都很陌生,但到目前为止,我已经能够很好地找到我的方法。这个让我完全难过。
答案 0 :(得分:1)
所以在昨晚一点钟地讨论之后,我终于找到了解决方案并立即开始使用facedesk。我在SessionsController中使用了SessionsHelper,并且我在课堂外使用了include语句:
include SessionsHelper
class SessionsController < ApplicationController
def new
end
...
...
...
end
通过移动类中的include语句:
class SessionsController < ApplicationController
include SessionsHelper
def new
end
...
...
...
end
我的所有测试都开始了。虽然我的测试在类块之外的include语句失败,但实际的功能似乎工作正常。我可以登录/退出,使用正确的信息存储cookie,我在服务器控制台中看不到任何错误。