Ruby on rails RSpec、工厂女孩和水豚:未保存任何项目
我有带RSpec的可安装Rails引擎:Ruby on rails RSpec、工厂女孩和水豚:未保存任何项目,ruby-on-rails,rspec,capybara,factory-bot,database-cleaner,Ruby On Rails,Rspec,Capybara,Factory Bot,Database Cleaner,我有带RSpec的可安装Rails引擎: RSpec.configure do |config| config.use_transactional_fixtures = false config.before(:suite) do DatabaseCleaner.clean_with(:truncation) end config.before(:each) do |example| DatabaseCleaner.strategy= example.meta
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do |example|
DatabaseCleaner.strategy= example.metadata[:js] ? :truncation : :transaction
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
简单工厂:
FactoryGirl.define do
factory :post, :class => MyEngine::Post do
title 'title'
end
end
水豚特征:
require 'spec_helper'
describe 'Post', :type => :feature do
let(:post) { FactoryGirl.create :post }
it 'index action should have post' do
visit posts_path
expect(page).to have_text(post.title)
end
end
Post模型没有任何验证
但当我运行测试时,它显示没有创建帖子
还有ActiveRecord日志:
INSERT INTO "my_engine_posts" ...
RELEASE SAVEPOINT active_record_1
rollback transaction
此规范将始终失败
让RSpec中的
是延迟加载<代码>发布只有在以下位置引用后才会实际创建:
expect(page).to have_text(post.title)
因此,您可以使用let代码>在访问页面之前,它不是延迟加载或参考post
:
require 'spec_helper'
describe 'Post', :type => :feature do
let(:post) { FactoryGirl.create :post }
it 'index action should have post' do
post
visit posts_path
expect(page).to have_text(post.title)
end
end
此规范将始终失败
让RSpec中的
是延迟加载<代码>发布
只有在以下位置引用后才会实际创建:
expect(page).to have_text(post.title)
因此,您可以使用let代码>在访问页面之前,它不是延迟加载或参考post
:
require 'spec_helper'
describe 'Post', :type => :feature do
let(:post) { FactoryGirl.create :post }
it 'index action should have post' do
post
visit posts_path
expect(page).to have_text(post.title)
end
end