kinsomicrote
kinsomicrote

Reputation: 187

Getting controller spec to pass

I am trying to get deep into testing, and I am having issues getting this show controller to pass. I've done all the tweaking I can think of.

spec/controllers/contacts_controller_spec.rb

describe "GET #show do" do
before do
  @contact = FactoryGirl.attributes_for(:contact)
end

it "assigns the requested contact to @contact" do
  get :show, user_id: user.id, id: contact
  expect(:contact).to be(contact)
end
end

Here is the factory

FactoryGirl.define do
  factory :contact do
    full_name      { Faker::Name.name}
    email          { Faker::Internet.email}
    sex            "male"
    date_of_birth  "19/02/1993"
    phone_number   { Faker::PhoneNumber.phone_number }
    user           { create :user }
    address        { Faker::Address.street_address }
    country        { Faker::Address.country }
    state          { Faker::Address.state }
    city           { Faker::Address.city }
    postal_code    { Faker::Address.postcode }
end

My show controller

def show
    @contact = Contact.find(params[:id])
 end

Upvotes: 0

Views: 41

Answers (1)

sadaf2605
sadaf2605

Reputation: 7540

Factory Girl's attributes_for returns the attributes for next sequence of factory. It does not create the object for you. So when you are making a get request with contact it is probably returning nil as it created only the attribute not the database entry.

So while you are testing your show, create your contact first, then check if your show is returning the correct entry or not.

before do
  @contact = FactoryGirl.create(:contact)
end

and if you are trying to pass user_id and id as get request parameters then it would be something like this:

it "assigns the requested contact to @contact" do
  params = {user_id: user.id, id: @contact.id}
  get :show, params
  expect(:contact).to be(@contact)
end

Upvotes: 1

Related Questions