0

每次我想创建一个Issue,我都需要链接UserLabel. 用户 has_many 标签和标签 has_many 用户。

问题是每次创建Issue.

let(:label) { Fabricate(:label) }
let(:responsible) { Fabricate(:responsible) }

before do
  label.stub(:users).and_return([responsible])
  responsible.stub(:labels).and_return([label])
end

let(:issue) { Fabricate(:issue, label: label, responsible: responsible)

我应该把它放在帮助类中吗?我想把它弄干。

4

1 回答 1

1

这个宝石可以做的事情:https ://github.com/thoughtbot/factory_girl

factory :label do
  name    "label example"
  user
end

factory :user do
  name    "John Doe"
  after(:create) do |user|
    FactoryGirl.create_list(:label, 1, user: user)
  end
end

factory :issue do
  name 'issue'
  after(:create) do |issue|
    issue.users = FactoryGirl.create_list(:user, 1)
  end
end

之后你可以做这样的事情:

issue = FactoryGirl.create(:issue)
issue.users # returns array with 1 user
issue.users.first.label # returns array with one label
于 2013-09-02T08:35:32.417 回答