My rspec test case is returning
expected: 1 time with any arguments
received: 0 times with any arguments
this error, even though the method gets called by the specific active record.
here is my rspec:
context "when the City has updated" do
let!(:city) {create(:city, ...}
let!(:user) {create(:user, city: city)
let!(:user2) {create(:user, city: city)
it "should call the process_scheudle_period for each of the user" do
expect(user).to receive(:create_new_period_for_mayor)
expect(user2).to receive(:create_new_period_for_mayor)
city.update_attribute(:mayor, "mr alford")
Delayed::Worker.new.run Delayed::Job.find_by(process_identifier: "process_city_changes")
end
end
the create_new_period_for_mayor being called in delayed job. so i run the delayed job as well.
in city.rb
after save call backs
self.delay(process_identifier: "process_city_changes").process_city_data_changes
def process_city_data_changes
users = self.users
users.each do |user|
user.create_new_period_for_mayor
end
end
i have used the debugger, and for each record the method is being called, but my test always returns
expected 1 time with any argument , recieved 0 times..
when i use expect_any_instance_of(User) then its getting passed.
but i need to expect for each user, specifically.
and also i have verified the user, which calls the create_new_period_for_mayor is the same user which i'm expecting.
where i'm getting wrong?..any help!
Behind the scenes, when a job is enqueued, what happens is that DJ stores the primary key and class of the model object you're using. Then when the job is run it creates a new instance of the object using those parameters.
...ie. the object when the job is run is not the same instance as the one you enqueued it on...
So yeah, use
_any_instance_of