我无法让我的测试套件工作。我尝试混合和匹配不同的配置。但是这个视图的结果都是一样的:要么在测试之间没有清理任何东西,要么数据库是空的。
我收到这种错误:
当什么都没有清理
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。我尝试了不同来源的不同配置,例如:
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
这有什么建议吗?