RSpec,工厂女孩和水豚:没有保存物品
RSpec, Factory Girl and Capybara: no items saved
我有可安装的 Rails 引擎 RSpec:
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
这个规范总是会失败。
let
in RSpec 是延迟加载。 post
在您引用它之前不会真正创建:
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
我有可安装的 Rails 引擎 RSpec:
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
这个规范总是会失败。
let
in RSpec 是延迟加载。 post
在您引用它之前不会真正创建:
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