【问题标题】:Trying to test omniauth with rspec & Capybara, failing尝试使用 rspec 和 Capybara 测试omniauth,失败
【发布时间】:2013-04-18 02:57:52
【问题描述】:

使用 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 以在 Rspec.configure do 块中包含 config.include IntegrationSpecHelper, :type => :request

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(试图让事情变得简单)。

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

【问题讨论】:

    标签: ruby-on-rails rspec capybara


    【解决方案1】:

    如果您尝试使用来自 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 =&gt; :request)

    收件人:

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

    【讨论】:

      【解决方案2】:

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

      config.include IntegrationSpecHelper, :type => :request
      

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

      解决方案:

      config.include IntegrationSpecHelper, :type => :feature
      

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

      问候, C-

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多