I’m attempting to test links that should/shouldn’t be seen when logged in, but RSpec is throwing the following error:
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)>'
My test block looks like so:
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
And my sign_in function:
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
I tried changing out
click_button "Log me in!"
with
find(:button, "Log me in!").click
and it results in the following error:
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)>'
I then attempted to replace my button text with “Log me in”, without the exclamation point,
and Capybara throws an ElementNotFound error:
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)>'
I’m still pretty new to RSpec and Rails in general, but I’ve been able to find my way around pretty well so far until now. This one has me completely stumped.
So after monkeying around a little bit last night, I finally found the solution and promptly began to facedesk. I’m using a SessionsHelper with my SessionsController, and I had my include statement outside of the class:
By moving the the include statement inside of the class:
All of my tests began to pass. While my tests were failing with the include statement outside of the class block, the actual functionality seemed to be working fine. I could sign in/out, the cookie was being stored with the correct information, and I was seeing no errors whatsoever in the server console.