【问题标题】:No database cleaning or empty database没有数据库清理或空数据库
【发布时间】:2013-03-28 20:29:43
【问题描述】:

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

我收到这种错误:

什么都没有清理

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"

或者这个,一个空的数据库:

 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

有什么建议吗?

【问题讨论】:

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


    【解决方案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 助手,例如 let(:group) { Group.create(name: "Group g") }

    【讨论】:

    • 感谢您的回答。事实是我已经尝试过你提到的两种解决方案。而且我不断遇到这两个错误之一。
    • 嗯,再次感谢。但我已经尝试过了。不知道问题出在哪里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多