Reputation: 2872
I'm writing a pretty straightforward test for a model scope:
describe "scope tests" do
let(:account1) { create(:account, last_seen: Date.today)}
let(:account2) { create(:account, last_seen: Date.today - 4.days)}
let(:account3) { create(:account, last_seen: Date.today - 10.days)}
let(:d) { Date.today}
let(:seen_between) { Account.seen_between(d - 14.days, d - 2.days) }
it "response to seen_between" do
expect(seen_between).to eq([account3, account2])
expect(seen_between).to_not include(account1)
end
end
But I'm getting some very verbose error:
Failure/Error: expect(Account.all).to eq([account3, account2])
expected: [#<Account id: 2, date_of_birth: nil, description: nil, active: true, created_at: "2018-05-10 12:05:4..._lead_id: nil, account_category_id: nil, psychiatrisch: nil, oggz: nil, problem: nil, actions: nil>]
got: #<ActiveRecord::Relation [#<Account id: 3, date_of_birth: nil, description: nil, active: true, create...lead_id: nil, account_category_id: nil, psychiatrisch: nil, oggz: nil, problem: nil, actions: nil>]>
Also, when I change the order of the assertion to [account2, account3] it still doesn't work. What is the correct way to make this test past (assuming that the scope does indeed returns the correct records, which it already does)
I also tried using inlcudes(), which of course works, but doesn't check for the correct order of records.
Thanks
Upvotes: 0
Views: 2852
Reputation: 2877
Try to use let!
, #to_a
and match_array
:
let(:today) { Date.today }
let!(:account1) { create(:account, last_seen: today) }
let!(:account2) { create(:account, last_seen: today - 4.days) }
let!(:account3) { create(:account, last_seen: today - 10.days) }
subject { Account.seen_between(today - 14.days, today - 2.days).to_a }
it { is_expected.to match_array [account2, account3] }
P.S. With match_array
you need no extra expect(...).not_to include
.
Upvotes: 6
Reputation: 36860
You're getting an active record relation but comparing it to an array.
Just convert the active record relation into an array.
it "response to seen_between" do
expect(seen_between.to_a).to eq([account3, account2])
expect(seen_between.to_a).to_not include(account1)
end
If you don't care about record order...
it "response to seen_between" do
expect(seen_between.to_a).to match_array([account3, account2])
expect(seen_between.to_a).to_not include(account1)
end
Upvotes: 2