我是 Rails 新手,目前正在尝试为提交表单编写测试。它应该测试表单是否可以为 Deal 模型创建新记录。它失败了,因为显然我试图创建的社区对象实际上并没有出现在下拉列表中。我想知道是否由于某种原因没有创建记录,或者没有保留。
我得到的错误是
Failure/Error: select community.name, :from => "deal[community_id]"
Capybara::ElementNotFound:
Unable to find option "Community7"
这是我的规范的相关部分:
describe "Deal Pages" do
subject {page}
describe "create Deal" do
let(:user) { FactoryGirl.create :user, username: "user_1", email: "user_1@email.com", password: "password" }
let(:community) { FactoryGirl.create :community, name: "Community7", title: "Community 7", description: "A great community", user: user }
before(:each) do
sign_in user
visit new_deal_path
end
let(:submit) { "Submit Deal" }
describe "with invalid information" do
it "should not create a Deal" do
expect { click_button submit }.not_to change(Deal, :count)
end
describe "after submission" do
before { click_button submit }
it { should have_title('Submit A New Deal') }
it { should have_content('error') }
end
end
describe "with valid information" do
it "should create a Deal" do
fill_in "Title", with: "A New Deal"
select community.name, :from => "deal[community_id]"
fill_in "Url", with: "http://www.amazon.com"
fill_in "Description", with: "This is the best Deal!"
expect { click_button submit }.to change(Deal, :count).by(1)
end
end
end
我正在使用 database_cleaner gem,我知道配置它会在每个示例之后清理数据库记录。无论出于何种原因,这件事都给我带来了很多麻烦。
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'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Devise support
#config.include Devise::TestHelpers, :type => :controller
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
Rails.logger.debug "DB Clean start"
end
config.after(:each) do
DatabaseCleaner.clean
Rails.logger.debug "DB Clean clean"
end
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
#for database_cleaner
config.use_transactional_fixtures = false
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
# Include the Capybara DSL so that specs in spec/requests still work.
config.include Capybara::DSL
# Disable the old-style object.should syntax.
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
工厂.rb
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "Person_#{n}" }
sequence(:email) { |n| "person_#{n}@example.com" }
password "password"
password_confirmation "password"
end
factory :community do
sequence(:name) { |n| "Community#{n}" }
sequence(:title) { |n| "Community #{n} Title" }
sequence(:description) { |n| "a great community #{n}" }
user
end
factory :deal do
sequence(:title) { |n| "Great Deal #{n}" }
url "http://www.amazon.com"
sequence(:description) { |n| "deal #{n} rocks so click it" }
user
community
end
end