我想測試一些回調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
任何幫助,將不勝感激! :)