Closed tinaoct1 closed 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??
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 :contact
factory?
Thanks.. :)
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
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)>'
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