我想测试一些回调before_save逻辑。但是我在这个黑暗的地方堆放着我找不到解决方案。rspec测试回调工作
我有这种方法,更新一些属性保存前:
def order_item_positions
Place.item_positions_reorder(item_position, city_id).each do |place|
new_item_position = place.item_position + 1
place.update_attributes(item_position: new_item_position)
end
end
该方法做什么,是改变上面+1位置的所有记录! ,比我想用RSpec的这样的事情来测试它:
describe "places order" do
let(:city){FactoryGirl.create(:city)}
let(:place){FactoryGirl.create(:place, city_id: city.id)}
let(:place_sec){FactoryGirl.create(:place, city_id: city.id)}
context "when placed before other record" do
it "should be placed before" do
place_sec.item_position = 1
place.item_position = 1
expect{
...somehow saving and triggering callback! //dont know how to do that :/
}.to change(place_sec, :item_position).from(1).to(2)
end
end
end
任何帮助,将不胜感激! :)