2012-03-15 111 views
0

我有一個幫助程序方法的Rspec測試,它需要訪問由Devise提供的我的current_user方法。問題是當我在我的測試中使用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

谷歌把我帶到這裏,但答案上面沒有幫助我,多一點研究,我發現下面的博客後。

我的錯誤:

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

希望你找到這個有用。

相關問題