使用带有Capybara和Rspec的Devise测试Rails应用程序不会导致方法错误

时间:2013-01-04 23:14:32

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

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

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

到我的spec / support / devise.rb文件。 我还在我的gemfile中添加了Capybara到测试组,运行bundle命令等。但是当我在我的测试中访问时,我得到一个no方法错误。这是我的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)>'

2 个答案:

答案 0 :(得分:1)

对于我使用的水豚规格:

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

  it "..."
end

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

答案 1 :(得分:1)

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


     group2.add(fs1, { flex: 1 });
     group2.add(fs);