繁体   English   中英

如何获得一个简单的测试来运行rspec + Fabric?

[英]How to get a simple test to run with rspec + fabrication?

我正在尝试进行简单的测试,并使用rspec + Fabrication运行。 不幸的是,关于这一点的文章不多。

在spec / model / event_spec.rb中

require 'spec_helper'

describe Event do
  subject { Fabricate(:event) }

  describe "#full_name" do
    its(:city) { should == "LA" }
  end

end

在spec / fabricators / event_fabricator.rb中

Fabricator(:event) do
  user { Fabricate(:user) }

  # The test works if I uncomment this line:
  # user_id 1

  city "LA"
  description "Best event evar"
end

在spec / fabricators / user_fabricator.rb中

Fabricator(:user) do
  name 'Foobar'
  email { Faker::Internet.email }
end

我不断得到:

 1) Event#full_name city 
     Failure/Error: subject { Fabricate(:event) }
     ActiveRecord::RecordInvalid:
       Validation failed: User can't be blank

PS,如果有人知道任何在线文章/教程,值得一读rspec和制造入门。 让我知道

Fabricator的功能之一是它懒惰地生成关联,这意味着除非在Event模型上调用user访问器,否则不会生成您的User

看来您的Event模型具有验证,需要User在场。 如果是这种情况,则需要这样声明您的制造者:

Fabricator(:event) do
  # This forces the association to be created
  user!
  city "LA"
  description "Best event evar"
end

这样可以确保与Event一起创建User模型,从而允许您的验证通过。

请参阅: http//fabricationgem.org/#!defining-fabricators

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM