尝试使用rspec和Capybara测试omniauth,失败

ezu*_*zuk 3 rspec ruby-on-rails capybara

使用Rails 3.2以及最新的Rspec和Capybara,这意味着我的Capybara规格位于spec/features

我真的是Rails和测试的新手,但我想习惯测试。在测试之前,我最终实现了OAuth。我终于使它工作了,现在我正在尝试进行追溯测试(所以我至少知道将来是否会失败)。我正在尝试按照本教程进行操作,但操作不正常。这是我所做的:

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.configuredo块内。

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
Run Code Online (Sandbox Code Playgroud)

当然会失败(我的应用程序中没有老虎),但我希望它继续失败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(试图使事情保持简单)。

知道可能是什么问题吗?提前致谢!

小智 5

如果您尝试使用Google的oauth,则需要更改:

def login_with_oauth(service = :google)

至:

def login_with_oauth(service = :google_oauth2)

:google_oauth2也应该是OmniAuth.config.add_mock的第一个参数,即:

OmniAuth.config.add_mock(
    :google_oauth2, 
    {
        :info => {
        :email => 'test@some_test_domain.com',
        :name=>'Test User'
    }
})
Run Code Online (Sandbox Code Playgroud)

不要忘记更改:

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

至:

config.include(IntegrationSpecHelper, :type => :feature) 如上文Christoph所述,在RSpec.configure块中。