Reputation: 48555
I have an Rspec test for a helper method which requires access to my current_user
method provided by Devise. The issue is when I use the login_user
macro in my tests for helpers they don't work!
Here is what my test looks like:
describe 'follow_link' do
before :each do
login_user
end
it "display 'follow' if the curren_user is not following" do
user = Factory :user
helper.follow_link(user).should == 'Follow'
end
end
But it fails with this:
Failure/Error: login_user
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x007faf8c680090>
# ./spec/support/macros.rb:4:in `login_user'
# ./spec/helpers/users_helper_spec.rb:29:in `block (3 levels) in <top (required)>'
And that macro looks like this:
def login_user
@user = Factory(:user)
visit new_user_session_path
# fill in sign in form
within("#main_container") do
fill_in "user[email]", with: @user.email
fill_in "user[password]", with: @user.password
click_button "Sign in"
end
end
I've required:
require 'spec_helper'
in my test and everything but that method still isn't available.
Upvotes: 1
Views: 1673
Reputation: 696
Google brought me here, but the answers above didn't help me, after a bit more research I found the blog below.
My Error:
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa49a73c>
Since Capybara 2.0 one has to use folder spec/features
capybara commands don't work in folder spec/requests
anymore.
Blog that helped me: http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html
Hope you find this usefull.
Upvotes: 1
Reputation: 6728
Friend 'visit' is the method of Capybara which is used for writing integration test cases.
For writing RSpec unit test cases you need to stub the current_user method call and focus on the functionality of the helper method.
describe 'follow_link' do
before :each do
@user = Factory :user
helper.stub(:current_user).and_return(@user)
end
it "display 'follow' if the curren_user is not following" do
helper.follow_link(@user).should == 'Follow'
end
end
Upvotes: 2
Reputation: 19418
Generally, in such cases, I mocking controller methods: mock(current_user){nil}
Upvotes: 1