没有数据库清理或空数据库

时间:2013-03-28 20:29:44

标签: ruby-on-rails rspec capybara spork database-cleaner

我无法让我的测试套件工作。 我试图混合搭配不同的配置。但是这个视图的结果都是一样的:要么在测试之间没有清理,要么数据库都是空的。

我遇到了这种错误:

什么都没有清理

1) Authentication Sign in page as new user with valid information creates a user
     Failure/Error: select group.name, from: 'user_group_id'
     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching option "Group g"

或者这是一个空DB:

 1) Authentication Sign in page as new user with valid information creates a user
     Failure/Error: select group.name, from: 'user_group_id'
     Capybara::ElementNotFound:
       Unable to find option "Group g"

我尝试过使用和不使用database_cleaner。我尝试过没有Spork。我尝试过不同来源的不同配置,例如:

http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

https://github.com/bmabey/database_cleaner#rspec-example

但我无法想象如何让它发挥作用。

这是Gemfile,带有gems版本:

gem 'rails', '3.2.13'
gem 'sqlite3'

group :development, :test do
  gem 'libnotify'
  gem 'rb-inotify'
  gem 'rspec-rails'       (2.13.0)
  gem 'guard-rspec'       (2.5.1)
  gem 'guard-spork'       (1.5.0)
  gem 'guard-livereload'   
  gem 'rack-livereload'    
end

group :test do
  gem 'capybara'          (2.0.2)
  gem 'launchy'             
  gem 'database_cleaner'  (0.9.1)
  gem 'factory_girl_rails'
end

以下是观点:

<%= form_for(@user, url: sessions_path ) do |f| %>

  <div class="field">
    <%= f.label :group_id %><br />
    <%= f.collection_select :group_id, Group.order('name asc').all, :id, :name, :prompt => "Choose your Group" %>
  </div>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :phone %><br />
    <%= f.text_field :phone %>
  </div>
  <div class="actions">
    <%= f.submit 'Go !'%>
  </div>
<% end %>

两个模特:

class Group < ActiveRecord::Base
  has_many :users
  attr_accessible :name, :phone, :active
end




class User < ActiveRecord::Base
  belongs_to :group
  attr_accessible :name, :phone, :group_id

  validates_presence_of :name, :phone, :group
end

以及相应的规范

require 'spec_helper'

describe "Authentication" do

  subject { page }
  before { visit root_path }

  describe "Sign in page" do
    let(:submit) {'Go !'}
    let(:session_new_content) { "Who's nearby"}
    let(:session_show_content){ "session show"}

    describe "as new user" do      
      describe "with valid information" do
        group = Group.create(name: "Group g")

        before do
          select group.name, from: 'user_group_id'
          fill_in 'Name', with: 'Samy'
          fill_in 'Phone', with: '0642604568'
        end

        it "creates a user" do
          expect{ click_button submit }.to change(User, :count).by(1)
        end

        describe "after saving the user" do 
          let(:user) { User.find_by_phone '0643614864'}  
          before do
            click_button submit
          end
          it { should have_content session_show_content}
          it { should have_content user.name}
        end
      end
    end
  end
end

这是我的spec_helper.rb

require 'rubygems'
require 'spork'


Spork.prefork do

  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|



    config.use_transactional_fixtures = false

    config.infer_base_class_for_anonymous_controllers = false

    config.order = "random"
    config.include Rails.application.routes.url_helpers

    config.include Capybara::DSL

    config.before(:suite) do
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end
  end

end

Spork.each_run do
  FactoryGirl.reload
end

这是什么建议?

1 个答案:

答案 0 :(得分:1)

问题似乎在这里:

describe "as new user" do      
  describe "with valid information" do
    group = Group.create(name: "Group g")

    before do

您正在创建一个在before块之外的组,因此它是在文件加载时创建的(在事务之外)。

为了让所有内容变得愉快,您需要在group = Group.create(name: "Group g")块内或before块内移动it调用。

编辑: 或者使用诸如的帮手 let(:group) { Group.create(name: "Group g") }