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.
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.