【发布时间】:2014-07-04 03:10:15
【问题描述】:
我正在努力养成编写规范的习惯,但是,这变得越来越令人沮丧。
假设我有两个简单的模型:User 和 Story。每个模型都使用belongs_to 关系。每个模型也使用validates :foo_id, presence: true。
但是,FactoryGirl 正在创建多个记录。
FactoryGirl.define do
factory :user do
email "foo@bar.com"
password "foobarfoobar"
end # this creates user_id: 1
factory :story do
title "this is the title"
body "this is the body"
user # this creates user_id: 2
end
end
这个简单的测试失败了:
require 'rails_helper'
describe Story do
let(:user) { FactoryGirl.create(:user) }
let(:story) { FactoryGirl.create(:story) }
it 'should belong to User' do
story.user = user
expect(story.user).to eq(user)
end
end
我在这里缺少什么?如果没有User,我无法建立Story 工厂,但我需要它只是一个User 记录。
【问题讨论】:
-
也显示失败的测试报告..
标签: ruby-on-rails ruby rspec factory-bot