Devitsが提供するcurrent_user
メソッドへのアクセスを必要とするヘルパーメソッドのRspecテストがあります。問題は、自分のテストでlogin_user
マクロを使用して、うまくいかないヘルパーを使用している場合です。Rspecヘルパーテストはコントローラメソッドへのアクセスが必要です
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
しかし、これで失敗します:
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)>'
そして、そのマクロは次のようになります。
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
私が必要としました。ここ
は私のテストは、次のようになります:
require 'spec_helper'
私のテストでは、その方法を除いてすべてが利用できません。