我是Rails的初学者,帮助我,我不明白问题是什么。看起来之前写的是其他东西(:每个)...... 如何在test_spac.rb中编写一个没有创建Card对象的测试。之前的方法(:each)不适用于此测试。
这是工作:
test_spec.rb:
require 'rails_helper'
describe "Index Page" do
before(:each) do
@card = FactoryGirl.create(:card)
DatabaseCleaner.start
end
it "shows error messages if translation is wrong" do
visit root_path
fill_in 'translated_text', with: 'wqww'
click_on 'Проверить'
expect(page).to have_content('Неверно!')
end
after(:each) do
DatabaseCleaner.clean
end
end
rails_helper.rb:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
require 'database_cleaner'
Capybara.javascript_driver = :poltergeist
Capybara.default_driver = :poltergeist
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.before(:each) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each, type: :features) do
DatabaseCleaner.strategy = :truncation
end
config.infer_spec_type_from_file_location!
end
这不起作用:
test_spec.rb:
require 'rails_helper'
describe "Index Page" do
before(:each) do
@card = FactoryGirl.create(:card)
end
it "shows error messages if translation is wrong" do
visit root_path
fill_in 'translated_text', with: 'wqww'
click_on 'Проверить'
expect(page).to have_content('Неверно!')
end
end
rails_helper.rb:
config.use_transactional_fixtures = false
config.before(:each) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each, type: :features) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
答案 0 :(得分:1)
问题解决了:
我这样做:(test_spec.rb) 要求' rails_helper'
describe "Index Page" do
it "check behavior then translation is wrong" do
FactoryGirl.create(:card) #################
visit root_path
fill_in 'translated_text', with: 'some text'
click_on 'Проверить'
expect(page).to have_content('Неверно!')
end
it "check page then object is not created" do
visit root_path
expect(page).to have_content('Непросмотренных')
end
end
答案 1 :(得分:0)
你可以发布不工作的意思吗?您收到任何错误消息吗?
顺便问一下,为什么config.use_transactional_fixtures
被禁用?如果您坚持使用before(:each)
挂钩,RSpec将reset the database for you,您不需要使用DatabaseCleaner
。