簡體   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