本文介绍了RSpec测试因FactoryGirl而失败,孤立时通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个应用程序从Rails 3升级到Rails 4,并具有一个名为 item 的模型,该模型具有以下架构:

 #===架构信息#表格名称:项目##type:字符串#成熟度:字符串 

以及经过一系列测试的规范,但特别是我遇到的一个问题:

 描述'next_version'做它应返回更高版本的商品的created_at日期"首先= FactoryGirl.create(:item,成熟度:'PRODUCTION')秒= FactoryGirl.create(:item,成熟期:'PREPRODUCTION',created_at:Time.now.in_time_zone)期望(first.next_version_created_at.to_s)等于second.created_at.utc.to_s) 

修复程序,我相信很容易..只需添加..

  created_at:Time.now.in_time_zone 

..到Production项并完成.但是,如果我自己运行它,则该测试通过,并且在Rails 3中通过了整个规范(146个测试),但是,当我现在运行整个规范时,除非我单独运行它们,否则这一种情况和其他3种类似情况都将失败.>

有想法吗?

解决方案

您可以在每次测试之前尝试重新加载Factory girl.尝试将其添加到spec_helper.rb

  config.before(:each)做FactoryGirl.reload结尾 

I am upgrading an app from Rails 3 to Rails 4 and have a model called item with the following schema:

#===Schema Information
# Table name: items
#
#type :string
#maturity: string

and a spec with a series of test, but this one in particular is the one I have the issue with:

describe 'next_version' do
it 'should return the created_at date of the Item one version higher' do
first = FactoryGirl.create(:item, maturity: 'PRODUCTION')
second = FactoryGirl.create(:item, maturity: 'PREPRODUCTION', created_at: Time.now.in_time_zone)

expect(first.next_version_created_at.to_s)to eq second.created_at.utc.to_s)

The fix, I believe is easy enough.. just add..

 created_at: Time.now.in_time_zone

..to the Production item and done. However, the test passes if I run it by itself, and the entire spec passed (146 tests) in Rails 3, however when I run the entire spec now this one and 3 other similar cases fail, unless I run them alone.

Thoughts?

解决方案

You could try reloading factory girl before each test. Try adding this to the spec_helper.rb

config.before(:each) do
  FactoryGirl.reload
end

这篇关于RSpec测试因FactoryGirl而失败,孤立时通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:08