2012-12-10 84 views
3

使用Rails 3.2和最新的Rspec和Capybara,這意味着我的水豚規格生活在spec/features試圖用rspec&水豚測試omniauth,失敗

我對Rails和測試非常陌生,但我想習慣測試。在測試之前,我最終實現了OAuth。我終於搞定了,現在我正在追溯測試它(所以我至少知道它是否會在未來破裂)。我正在嘗試關注this tutorial,但事情並不奏效。下面是我所做的:

1)創建spec/support/integration_spec_helper.rb有:

module IntegrationSpecHelper def login_with_oauth(service = :google) visit "/auth/#{service}" end end

2)修改spec/spec_helper包括config.include IntegrationSpecHelper, :type => :requestRspec.configure內做塊。

3)創建spec/features/omniauth_spec.rb有:

require 'spec_helper' 
feature 'testing oauth' do 
    scenario 'should create a new tiger' do 
    login_with_oauth 
    visit new_tiger_path 

    fill_in 'tiger_name', :with => 'Charlie' 
    fill_in 'tiger_blood', :with => 'yes' 

    click_on 'Create Tiger' 

    page.should have_content("Thanks! You are a winner!") 
    end 
end 

當然,這是要失敗的(我沒有在我的應用程序老虎),但我希望它失敗的visit new_tiger_path中。相反,運行規範,我得到:

1) testing oauth should create a new tiger Failure/Error: login_with_oauth NameError: undefined local variable or method `login_with_oauth' for #<RSpec::Core::ExampleGroup::Nested_3:0x83355d8> # ./spec/features/omniauth_spec.rb:4:in `block (2 levels) in <top (required)>'

因此,基本上,它說,有沒有這樣的事情login_with_oauth。這一定是一個非常基本的錯誤,因爲我的代碼由於某種原因未被包含。

我沒有使用spork(試圖保持簡單)。

任何想法可能是什麼問題?提前致謝!

回答

0

有點晚了,但也許我可以幫忙。 遇到同樣的問題。這是由於

config.include IntegrationSpecHelper, :type => :request 

因爲您編寫rspec功能測試,參數':type'需要更改爲':feature'。

解決方案:

config.include IntegrationSpecHelper, :type => :feature 

不幸的是這將導致更多的問題,我解決不了呢。

問候, C-

4

如果你試圖使用從谷歌的OAuth,你會想改變:

def login_with_oauth(service = :google)

到:

def login_with_oauth(service = :google_oauth2)

:google_oauth2 sh也應該是OmniAuth.config.add_mock的第一個參數,即:

OmniAuth.config.add_mock(
    :google_oauth2, 
    { 
     :info => { 
     :email => '[email protected]_test_domain.com', 
     :name=>'Test User' 
    } 
}) 

不要忘了改:

config.include(IntegrationSpecHelper, :type => :request)

到:

config.include(IntegrationSpecHelper, :type => :feature) 的RSpec.configure塊內,如克里斯托弗如上所述。