RSPEC error: expected #<> got #<> (compared using ==) - Ruby on Rails

I am testing my controller with RSPEC in ruby on rails. Here is the (relevant parts of the) controller action i am testing,

Controller Code:

 before do 
    @company=FactoryGirl.create(:company)
    @customer=FactoryGirl.create(:customer, company_id: @company.id)
    @job=FactoryGirl.create(:job, customer_id: @customer.id, company_id:  @company.id)
  end

class JobsController < ApplicationController
  def new
    if params[:customer_id]
      @job = current_member.company.jobs.new(
        customer_id: params[:customer_id],
        lead_id: params[:lead_id]
      )
     else
      ...
     end
    ...
  end

RSPEC Code:

   it "has valid job with customer_id param" do 
      get :new, {:customer_id=>@customer.id, :lead_id=>@job.lead_id}
      expect(assigns(:job)).to eq @member.company.jobs.new(customer_id:@customer.id, lead_id:@job.lead_id)
    end

Here is the error I am getting:

Failures:

   1) Failure/Error: expect(assigns(:job)).to eq @member.company.jobs.new(customer_id:@customer.id, lead_id:@job.lead_id)

   expected: #<Job id: nil, name: nil, status: "pending", company_id: 43, account_id_old: nil, job_type_id: nil, address_id: nil, trade_id: nil, lead_id: nil, started_date: nil, end_date: nil, created_at: nil, updated_at: nil, order_number: nil, creator_id: nil, account_id: nil, customer_id: 22, contact_id: nil>
        got: #<Job id: nil, name: nil, status: "pending", company_id: 43, account_id_old: nil, job_type_id: nil, address_id: nil, trade_id: nil, lead_id: nil, started_date: nil, end_date: nil, created_at: nil, updated_at: nil, order_number: nil, creator_id: nil, account_id: nil, customer_id: 22, contact_id: nil>

   (compared using ==)

I dont get it, both the 'expected' and the 'got' sections appear to be the same thing! Ideas/Help please?

Answers


Just because the data is the same, doesn't mean they're the same object (which they're not).

You really need to check the various things about your assigned object individually, eg:

before { get :new, { customer_id: @customer.id, lead_id: @job.lead_id } }

subject(:job) { assigns :job }

it { is_expected.to be_a_new Job }

it "should have the right customer_id" do
  expect(job.customer_id).to eq @customer.id
end

it "should have the right lead_id" do
  expect(job.lead_id).to eq @job.lead_id
end

...something like that.


Since both objects are not saved, and you don't really want to test if they are exactly the same object, you could check if their attributes match, the comparison will be between two hashes

it "has valid job with customer_id param" do 
  get :new, {:customer_id=>@customer.id, :lead_id=>@job.lead_id}
  expect(assigns(:job).attributes).to eq @member.company.jobs.new(customer_id:@customer.id, lead_id:@job.lead_id).attributes
end

Need Your Help

github synchronization - upstream, origin and the local repository

git github fetch synchronize

I'm a noob at TortoiseGit/github and it's driving me crazy..

Need to manually delete a project from App Explorer

aptana

I deleted the .project file off the filesystem permanently and need to remove the subsequent project from the Aptana App Explorer so that I can re-create it with the same name. The option to "Delete