3

使用 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

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

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

4

2 回答 2

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'
    }
})

不要忘记更改:

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

至:

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

于 2013-04-17T18:57:52.143 回答
0

有点晚了,但也许我可以帮忙。遇到了同样的问题。它是由

config.include IntegrationSpecHelper, :type => :request

参数 ':type' 需要更改为 ':feature' 因为您编写了 rspec 功能测试。

解决方案:

config.include IntegrationSpecHelper, :type => :feature

不幸的是,这会导致更多问题,我还无法解决。

问候, C-

于 2013-03-13T10:46:37.527 回答