【问题标题】:Testing a Rails App using Devise with Capybara and Rspec causes no Method Error使用带有 Capybara 和 Rspec 的 Devise 测试 Rails 应用程序不会导致方法错误
【发布时间】:2013-01-04 23:14:32
【问题描述】:

我刚刚在我的 Rails 应用程序上开始了一些测试,我在其中使用 Devise 进行身份验证。我刚刚生成了 rspec 并且只添加了

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

到我的 spec/support/devise.rb 文件。 我还在我的 gemfile 中将 Capybara 添加到了测试组中,运行了 bundle 命令等。但是当我在测试中访问时,我得到了一个 no method 错误。这是我的 user_spec.rb 的示例:

require 'spec_helper'

describe "Club" do
  before(:each) do
    @club ||= FactoryGirl.create(:club)
    FactoryGirl.create(:membership)
    User.all.each{|x| x.delete}
  end
  describe "privacy" do
    it "shouldn't be shown any tournament if the user is private" do
      attributes = FactoryGirl.attributes_for(:user)
      user = User.create!({private: true}.merge(attributes))
      assert(user.private)
      visit "/user/#{user.id}/tournaments"

      page.should have_no_selector(".tournament-list")
    end
  end
end

当我运行 rspec 时,会出现这样的错误:

Failures:

  1) Club privacy shouldn't be shown any tournament if the user is private
     Failure/Error: visit "/user/#{user.id}/tournaments"
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_1:0x007ff0ea113108>
     # ./spec/user_spec.rb:14:in `block (3 levels) in <top (required)>'

【问题讨论】:

标签: ruby-on-rails-3 devise capybara rspec-rails


【解决方案1】:

对于我使用的水豚规格:

describe "Club", :type => :feature, :js => true do
  include Capybara::DSL

  it "..."
end

取而代之的是 User.all.each{|x| x.delete},使用DatabaseCleaner

【讨论】:

    【解决方案2】:

    您可以轻松地将 Capybara::DSL 添加到 RSpec 配置 spec_helper.rb

    RSpec.configure do |config|
      .....
      .......
      config.include(Capybara::DSL)
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 2023-03-13
      相关资源
      最近更新 更多