简体   繁体   English

试图用rspec和Capybara测试omniauth,失败了

[英]Trying to test omniauth with rspec & Capybara, failing

Using Rails 3.2 and the latest Rspec and Capybara, which means my Capybara specs live in spec/features . 使用Rails 3.2和最新的Rspec和Capybara,这意味着我的Capybara规格符合spec/features

I'm really new to Rails and testing, but I want to get used to testing. 我是Rails和测试的新手,但我想习惯测试。 I ended up implementing OAuth before testing it. 我在测试之前最终实现了OAuth。 I finally got it working, and now I'm trying to retroactively test it (so I at least know if it breaks in the future). 我终于让它工作了,现在我正在尝试追溯测试它(所以我至少知道它是否会在未来中断)。 I'm trying to follow this tutorial , but things aren't working. 我正在尝试按照本教程 ,但事情不起作用。 Here's what I did: 这是我做的:

1) Created spec/support/integration_spec_helper.rb with: 1)创建spec/support/integration_spec_helper.rb

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

2) Modified spec/spec_helper to include config.include IntegrationSpecHelper, :type => :request inside the Rspec.configure do block. 2)修改spec/spec_helper以包含config.include IntegrationSpecHelper, :type => :requestRspec.configure do块内的config.include IntegrationSpecHelper, :type => :request

3) Created spec/features/omniauth_spec.rb with: 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

Of course it's going to fail (I don't have tigers in my app) but I want it to fail on visit new_tiger_path . 当然它会失败(我的应用程序中没有老虎)但我希望它在visit new_tiger_path失败。 Instead, running the spec, I get: 相反,运行规范,我得到:

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

So basically, it says there's no such thing login_with_oauth . 所以基本上,它说没有这样的东西login_with_oauth This must be a really basic error, as my code isn't included for some reason. 这必须是一个非常基本的错误,因为我的代码由于某种原因不包括在内。

I'm not using spork (trying to keep things simple). 我没有使用spork(试图保持简单)。

Any idea what the problem might be? 知道问题可能是什么? Thanks in advance! 提前致谢!

If you are trying to use oauth from google, you'll want to change: 如果您尝试使用谷歌的oauth,您需要更改:

def login_with_oauth(service = :google)

to: 至:

def login_with_oauth(service = :google_oauth2)

:google_oauth2 should also be the first argument to OmniAuth.config.add_mock , ie: :google_oauth2也应该是第一个参数OmniAuth.config.add_mock,即:

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

Don't forget to change: 不要忘记改变:

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

to: 至:

config.include(IntegrationSpecHelper, :type => :feature) inside the RSpec.configure block, as Christoph noted above. 如上所述,在RSpec.configure块中的config.include(IntegrationSpecHelper, :type => :feature)

A little late, but maybe I can help. 有点晚了,但也许我可以帮忙。 Got the same problem. 得到了同样的问题。 It's caused by 这是由

config.include IntegrationSpecHelper, :type => :request

The paramater ':type' needs to be changed to ':feature' because you write a rspec feature test. 参数':type'需要更改为':feature',因为您编写了一个rspec特性测试。

Solution: 解:

config.include IntegrationSpecHelper, :type => :feature

Unfortunately this causes further problems, I couldn't solve yet. 不幸的是,这会导致更多问题,我还无法解决。

Regards, C- 此致,C-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM