RSpec,工厂女孩和水豚:没有项目保存(RSpec, Factory Girl and Capybara: no items saved)

编程入门 行业动态 更新时间:2024-10-24 18:17:24
RSpec,工厂女孩和水豚:没有项目保存(RSpec, Factory Girl and Capybara: no items saved)

我有可安装的带有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.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

I have mountable Rails engine with 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

Simple factory:

FactoryGirl.define do factory :post, :class => MyEngine::Post do title 'title' end end

Capybara feature:

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

And Post model doesn't have any validations.

But when i running tests it shows that there is no posts created.

Also ActiveRecord logs:

INSERT INTO "my_engine_posts" ... RELEASE SAVEPOINT active_record_1 rollback transaction

最满意答案

这个规范总是会失败。

let 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

This spec will always fail.

let in RSpec is lazy loading. post is not actually created until you reference it in:

expect(page).to have_text(post.title)

So you can either use let! which is not lazy loading or reference post before you visit the page:

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

更多推荐

本文发布于:2023-07-16 22:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1135412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工厂   女孩   项目   RSpec   Factory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!