everydayrails / rails-4-1-rspec-3-0

Code samples for Everyday Rails Testing with RSpec, Rails 4.1/RSpec 3.0 edition
272 stars 229 forks source link

error with invalid contact for controller spec #67

Closed tinaoct1 closed 9 years ago

tinaoct1 commented 9 years ago

This is my Controller spec:

describe 'POST#create' do context "with valid attributes" do it "saves the new contact in the database" do expect { post :create, contact: attributes_for(:contact) }.to change(Contact, :count).by(1) end

        it "redirects to contacts#show" do
            post :create, contact: attributes_for(:contact)
            expect(response).to redirect_to contact_path(assigns[:contacts])
        end
    end

    context "with invalid attributes" do
        it "does not save the new contact in the database" do
            expect{
                post :create,
                contact: attributes_for(:invalid_contact)
            }.not_to change(Contact, :count)
        end

        it "re-renders the new template" do
            post :create, contact: attributes_for(:invalid_contact)
            expect(response).to render_template :new
        end
    end
end

I am getting the following error when i am running the POST#create for an invalid contact:

ContactsController GET#show assigns the requested contact to @contact renders the :show template GET#new assigns a new contact to @contact renders the :new template GET#edit assigns the requested contact to @contact renders the :edit template GET#index lists the entries in the phonebook renders the :index template POST#create with valid attributes saves the new contact in the database redirects to contacts#show with invalid attributes does not save the new contact in the database (FAILED - 1) re-renders the new template

Contact has a valid factory Contact is invalid without a name is invalid without a number is invalid if the phone number is not an integer is invalid with a duplicate phone number

Failures:

1) ContactsController POST#create with invalid attributes does not save the new contact in the database Failure/Error: contact: attributes_for(:invalid_contact) NameError: uninitialized constant InvalidContact

./spec/controllers/contacts_controller_spec.rb:84:in `block (5 levels) in <top (required)>'

 # ./spec/controllers/contacts_controller_spec.rb:82:in`block (4 levels) in <top (required)>'

Finished in 0.28958 seconds (files took 1.54 seconds to load) 17 examples, 1 failure

Failed examples:

rspec ./spec/controllers/contacts_controller_spec.rb:81 # ContactsController POST#create with invalid attributes does not save the new contact in the database

tinaoct1 commented 9 years ago

I have initialized the invalid contact in spec/factories/contacts.rb as follows:

FactoryGirl.define do factory :contact do name { Faker::Name.name} number { Faker::Number.number(10)} end

factory :invalid_contact do
    name nil
    number nil
end

end

Why is the error still coming??

ruralocity commented 9 years ago

Try this for your factory:

FactoryGirl.define do
  factory :contact do
    name { Faker::Name.name}
    number { Faker::Number.number(10)}

    factory :invalid_contact do
      name nil
      number nil
    end
  end
end

See how :invalid_contact is nested within the main :contactfactory?

tinaoct1 commented 9 years ago

Thanks.. :)