0

我刚刚开始对我使用 Devise 进行身份验证的 Rails 应用程序进行一些测试。我刚刚生成了 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)>'
4

2 回答 2

1

对于水豚规格,我使用:

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

  it "..."
end

而不是 User.all.each{|x| x.delete},使用 DatabaseCleaner

于 2013-10-22T04:39:05.217 回答
1

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

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

end
于 2016-04-29T14:01:15.350 回答