ruby on rails - Test order in has_many relationship with RSpec -
i'm not experienced rails , rspec , have troubles writing tests. so, next trouble don't know how test order in model's relationship properly.
let's have simple model this:
class kitchen < activerecord::base has_many :orders, -> { order(completed_at: :desc) } end
and simple test model:
require 'rails_helper' rspec.describe kitchen, :type => :model before { @kitchen = factorygirl.create(:kitchen) } subject { @kitchen } describe "orders" before @kitchen.orders.build(description: "test description 1", completed_at: 1.day.ago.localtime) @kitchen.orders.build(description: "test description 2", completed_at: time.now) end "should sorted completion date in descending order" expect(@kitchen.orders.first.completed_at).to > @kitchen.orders.last.completed_at end end end
as result have got error:
failure/error: expect(@kitchen.orders.first.completed_at).to > @kitchen.orders.last.completed_at expected: > fri, 15 may 2015 12:21:54 utc +00:00 got: thu, 14 may 2015 12:21:54 utc +00:00
any appreciated.
you using build
. not persist database, when call @kitchen.orders.first
, not hitting database, getting first 1 created, wrong one.
use create
instead, call @kitchen.reload
refresh database.
Comments
Post a Comment