FactoryGirl Has Many through
Asked Answered
D

2

0

Okay, so i've set up a many-to-many association for users, projects and the ability to watch those projects. (Called, not the best name I've ever come up with "Watchings")

#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

Functionally everything is working so far. I can watch projects and find out how many watchers a project has. But my cucumber feature keeps erroring out because I don't know how to make Factory Girl cooperate.

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

And my step definition:

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

Whenever the step definition runs, it panics out and says

undefined method `watching_user_id=' for #<Watching:0x007ff7800c7978> (NoMethodError)

I've tried a few different things, but I can't figure out how to make it actually make the association for the test.

Demagogue answered 27/12, 2011 at 23:10 Comment(1)
The error message I was getting was actually from the automatically generated factory that I didn't realize was being made.Demagogue
U
3

Note that you are using the older FactoryGirl Syntax for factory definition.

Try the following:

Factory.define :watchings do |watching|
  watching.association(:user)
  watching.association(:watched_project), :factory => :project
end

This should tell the watching factory that watched_project is actually of type project.

With the new syntax your full factory definition would look like:

FactoryGirl.define do
  factory :user do
    sequence(:email) {|n| "user#{n}@example.com"}
    password "secret"
    password_confirmation {|u| u.password }
  end

  factory :project do
    name "Project"
    description "Hello World"
  end

  factory :watchings do
    user
    association :watched_project, :factory => :project
  end
end

Which looks much cleaner.

Unmask answered 4/1, 2012 at 12:44 Comment(0)
H
2

You use a messy naming approach. Also, also here is the Factory Girl example that should work in:

FactoryGirl.define do

  factory :project do
    name "Project"
    description "Hello World"
  end

  factory :user do
    sequence(:email) { |n| "user#{n}@example.com" }
    sequence(:password) { |n| "password#{n}" }
  end

  factory :watching do
    user
    project
  end        
end
Heigho answered 3/1, 2012 at 16:45 Comment(1)
Yeah, I'm really not proud of the name "Watchings", but I couldn't think up a different name that wasn't as ugly. I'll give this a shotDemagogue

© 2022 - 2024 — McMap. All rights reserved.