helengutz
helengutz

Reputation: 55

FactoryGirl::InvalidFactoryError Controller Spec

I'm trying to create a controller spec for a contact form. All I want is to test that the :new template is rendered when invalid attributes are submitted. I've tried various configurations of the factory, as well as changing it to a trait, instead of factory to no avail. The error message doesn't get very specific, just Invalid Factory. Any ideas would be appreciated!

# blog_app/spec/factories/inquiries.rb

FactoryGirl.define do

  factory :inquiry do
    name "TestName"
    email "[email protected]"
    phone "123-456-7890"
    message "TestMessage"
  end

  factory :invalid_inquiry, parent: :inquiry do
    name nil
  end
end

#/app/controllers/inquiries_controller.rb

class InquiriesController < ApplicationController

def new
  @inquiry = Inquiry.new
end

def create
   @inquiry = Inquiry.new(params[:inquiry])
     if @inquiry.deliver
       render :thank_you
     else
       render :new
     end
   end
end

# spec/controllers/inquiries_controller_spec.rb

require 'spec_helper'

describe InquiriesController do
  it "has a valid factory" do
    FactoryGirl.build(:inquiry).should be_valid
end


describe "POST #create" do
  context "with valid attributes" do
    it "delivers inquiry" #pending

    it "renders the :thank_you page template" do
      post :create, inquiry: FactoryGirl.attributes_for(:inquiry)
      response.should render_template :thank_you
    end
  end

  context "with invalid attributes" do
    it "does not deliver inquiry" do #pending

    it "renders :new page template" do
      post :create, inquiry: FactoryGirl.attributes_for(:invalid_inquiry)
      response.should render_template :new
    end
  end
end

describe "GET #new" do
  it "renders :new page template" do
    get :new
    response.should render_template :new
  end
end
end

Finished in 0.31244 seconds
0 examples, 0 failures
/Users/.../.rvm/gems/ruby-2.0.0-p353/gems/factory_girl-4.4.0/lib/factory_girl.rb:73:in `lint': The following factories are invalid: (FactoryGirl::InvalidFactoryError)

* invalid_inquiry
from /Users/.../Documents/blog_app/spec/support/factory_girl.rb:8:in `block (2 levels) in <top (required)>'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:21:in `instance_eval'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:21:in `run'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:66:in `block in run'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:66:in `each'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:66:in `run'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:27:in `block in run'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /Users/.../.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'

Upvotes: 0

Views: 1860

Answers (1)

Steve Rowley
Steve Rowley

Reputation: 1578

Does this work?

FactoryGirl.define do

  factory :inquiry do
    name "TestName"
    email "[email protected]"
    phone "123-456-7890"
    message "TestMessage"

    factory :invalid_inquiry do
      name nil
    end
  end
end

Alternatively you might try replacing:

  post :create, inquiry: FactoryGirl.attributes_for(:invalid_inquiry)

with:

  post :create, inquiry: FactoryGirl.attributes_for(:inquiry, name: nil)

and getting rid of the invalid_inquiry factory altogether.

Upvotes: 1

Related Questions