Factory_bot: Undefined method error when creating new instances

Created on 19 Dec 2012  路  4Comments  路  Source: thoughtbot/factory_bot

I am having an issue with one of my factories that I can't seem to resolve, and it appears to be similar to the earlier-reported issue #290. However, I am unable to create the instance in any context. I have tried loading my factories into pry and testing this class, but to no avail.

Here are the classes I'm using, where I am seeing this issue:

# app/models/package.rb

require 'publicable'

class Package < ActiveRecord::Base
  resourcify
  include Publicable

  attr_accessible :name, :package_manager_id, :revision, :user_id, :version

  attr_accessible :user, :package_manager, :args

  belongs_to :user

  belongs_to :package_manager
  has_many :args, class_name: 'PackageArg'

  before_save { increment :revision }
end

Package.includes(:args)

# app/factories/packages.rb

FactoryGirl.define do
  factory :package do
    sequence(:name) { |n| "package-#{n}"}
    version '0.0.1'
    association :package_manager, factory: package_manager
    user { User.find_by_login('cohesiveft') }

    factory :public_package do
      public_at DateTime.now
    end

  end
end

# spec/controllers/packages_controller_spec.rb

require 'spec_helper'
require 'debugger'

describe PackagesController do
  login_user

  let(:package)    { FactoryGirl.create :public_package }
  subject          { package }

  context 'JSON' do

    describe "GET 'index'" do
      it "returns http success" do
        get :index, { format: 'json'}
        response.should be_success
        response.should render_template 'packages/index'
      end
    end

    describe "GET 'show'" do
      it "returns http success" do
        get :show, { id: package.id }
        response.should be_success
        response.should render_template 'packages/show'
      end
    end
  end

end

Here is what I'm seeing:

Failures:

  1) PackagesController JSON GET 'show' returns http success
     Failure/Error: let(:package)    { FactoryGirl.create :public_package }
     NoMethodError:
       undefined method `name' for :package:Symbol
     # ./spec/controllers/packages_controller_spec.rb:7:in `block (2 levels) in <top (required)>'
     # ./spec/controllers/packages_controller_spec.rb:22:in `block (4 levels) in <top (required)>'

Finished in 0.05674 seconds
2 examples, 1 failure

This app is still in development, so there may be some silly mistakes in it yet. Any thoughts?

Most helpful comment

Looks like in your factory, you have association :package_manager, factory: package_manager - package_manager needs to be a symbol, though. Let me know if that fixes things when you change it. Good luck!

All 4 comments

Looks like in your factory, you have association :package_manager, factory: package_manager - package_manager needs to be a symbol, though. Let me know if that fixes things when you change it. Good luck!

@pcmantz Closing this due to inactivity, but I'm still confident that the reference to the package_manager class needs to be a symbol. Please reopen this if changing that to a symbol doesn't fix it; good luck!

This ended up being my issue I spent a few hours trying to isolate. Might be worth trying to catch this error (using the name directly rather than the symbol equivalent) somehow and warn about it more sanely? Not sure how though. :/

This thread really saved my day. I was searching everywhere trying to solve this error.

Was this page helpful?
0 / 5 - 0 ratings