好吧,所以我建立了一個多對多的關聯用戶,項目和觀看這些項目的能力。 (被稱爲,而不是我曾經想出「Watchings」的最好名字)FactoryGirl有很多通過
#project.rb
class Project < ActiveRecord::Base
has_many :watchings, :foreign_key => "watched_project_id"
has_many :watchers, :through => :watchings, :source => :user
end
#user.rb
class User < ActiveRecord::Base
has_many :watchings
has_many :watched_projects, :through => :watchings
end
#watching.rb
class Watching < ActiveRecord::Base
belongs_to :user
belongs_to :watched_project, :class_name => 'Project'
end
功能上一切工作到目前爲止。我可以觀看項目並找出項目有多少觀衆。但我的黃瓜功能不斷出錯,因爲我不知道如何讓Factory Girl合作。
Factory.define :user do |user|
user.sequence(:email) {|n| "user#{n}@example.com"}
user.password "secret"
user.password_confirmation {|u| u.password }
end
Factory.define :project do |project|
project.name "Project"
project.description "Hello World"
end
Factory.define :watchings do |watching|
watching.association(:user)
watching.association(:watched_project)
end
我的步驟定義:
Given /^a Project exists called "(.+)" with (\d+) watchers$/ do |project, watchers|
@project = Factory(:project)
watchers.to_i.times do |watcher|
watcher = Factory(:user)
watching = Factory(:watching)
watching.user = watcher
watching.watched_project = @project
end
end
當步長定義運行時,它恐慌了,並說
undefined method `watching_user_id=' for #<Watching:0x007ff7800c7978> (NoMethodError)
我已經嘗試了一些不同的東西,但我可以」弄清楚如何使它實際上成爲測試的關聯。
我得到的錯誤信息實際上來自於我沒有意識到的自動生成的工廠。 – DVG 2012-01-07 09:26:17