2012-03-15 3 views
0

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' 

私のテストでは、その方法を除いてすべてが利用できません。

答えて

1

友人訪問 'は統合テストケースを記述するために使用されるカピバラの方法です。

RSpecユニットのテストケースを書くには、current_userメソッド呼び出しをスタブし、ヘルパーメソッドの機能に焦点を当てる必要があります。

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 
1

一般に、このような場合には、私はモックコントローラ方法:モック(CURRENT_USER){ニル}

1

Googleはここに私をもたらしたが、上記の答えは私を助けていない、もう少し研究の後、私は次のブログを見つけました。

マイエラー:

NoMethodError: 
     undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa49a73c> 

カピバラ2.0一つはspec/featuresカピバラコマンドはもうフォルダspec/requestsでは動作しないフォルダを使用する必要があるため。私を助けてくれ

ブログ:http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html

は、あなたがこの便利を見つけることを願っています。

関連する問題