Question

I am testing a single method in the model. It's called last_photo, I filled in the data of the database and try to return the first element, but I have an error udefined method 'latest'. What could it be? How can I fix it?

Method latest this: scope :latest, -> { order('created_at DESC') }

def last_photo
  @last_photo ||= user_updates.latest.where("photo_front IS NOT NULL and photo_front != ''").first.try(:photo_front)
end

context "instance method" do
  let(:user) { create :user }

  context "last photo" do
    before { create_list(:user_update, 3, user: user) }
    let(:user_updates){ UserUpdate.all }

    describe "#last_photo" do
      subject { user.last_photo }

      it { should eq user_updates.latest.first.photo_front }
    end

    describe "#last_photo_side" do
      subject { user.last_photo_side }

      it { should eq user_updates.latest.first.photo_side}
    end
  end
end

Thanks.

Was it helpful?

Solution

I bet UserUpdate.all returns an array. So you cant chain scopes on it.

Replace with:

let(:user_updates){ UserUpdate.scoped }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top