Pergunta

I have this problem with testing, i have logic that reorders positions, for example if i have 1, 2, 3 and i give for 3 position 1 than 1, 2 will become 2, 3. Now i am trying to test this but i cant seem to figure this out!

Here is my tests:

describe "order change" 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)}
    let(:place_th){FactoryGirl.create(:place, city_id: city.id)}

context "when editing record" do
      it "should go up and change middle records" do
        place
        place_sec
        place_th
        expect{
          place_id = Place.find(place.id)
          place_id.update_attributes(item_position: 3)
        }.to change(Place.find(place_sec.id), :item_position).from(2).to(1)
      end
    end

This does not work, what i need to do for this to work? Method for reordering is called with callback before_save

Foi útil?

Solução

Okey googling and stackoverflowing around i found that have to reaload object to get the results i need. Something like this:

describe "order change" 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)}
    let(:place_th){FactoryGirl.create(:place, city_id: city.id)}

context "when editing record" do
      it "should go up and change middle records" do
        place
        place_sec
        place_th
        expect{
          place.update_attributes(item_position: 3)
          place_sec.reload
        }.to change{place_sec.item_position}.from(2).to(1)
      end
    end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top