좋아, 그래서 사용자, 프로젝트 및 해당 프로젝트를 볼 수있는 능력을 위해 다 대다 관계를 설정했습니다. (불려지는 최고의 이름이 아닌 "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
그리고 내 단계 정의 :
단계 정의가 밖으로 패닉, 실행하고 나는 몇 가지 다른 일을 시도했습니다
undefined method `watching_user_id=' for #<Watching:0x007ff7800c7978> (NoMethodError)
말한다 때마다
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
하지만 내가 할 수있는 ' 실제로 테스트를위한 연관성을 만드는 방법을 찾아야합니다.
내가 얻고있는 오류 메시지는 실제로 자동 생성 된 공장에서 나왔다는 것을 깨닫지 못했다. – DVG