尝试用rspec&测试omniauth水豚,失败了

时间:2012-12-10 17:03:13

标签: ruby-on-rails rspec capybara

使用Rails 3.2和最新的Rspec和Capybara,这意味着我的Capybara规范存在spec/features

我是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 => :request阻止内包含Rspec.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(试图保持简单)。

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

2 个答案:

答案 0 :(得分:4)

如果您尝试使用谷歌的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)

to:

config.include(IntegrationSpecHelper, :type => :feature) 正如Christoph在上面提到的那样,在RSpec.configure块中。

答案 1 :(得分:0)

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

引起的
config.include IntegrationSpecHelper, :type => :request

参数':type'需要更改为':feature',因为你编写了一个rspec特性测试。

解决方案:

config.include IntegrationSpecHelper, :type => :feature

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

此致 Ç -