Reputation: 1623
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?
Upvotes: 0
Views: 2770
Reputation: 42909
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
Upvotes: 0
Reputation: 27971
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.
Upvotes: 1