You could also stub the .find in your controller and force the object in the action to be the double from your test
i.e. expect(User).to receive(:find).and_return(@user1) then your test would work assume kill was actually called on @user1 On Sunday, April 12, 2020 at 9:19:42 PM UTC-7, Jack R-G wrote: > > Consider the following controller test fragment: > > … > it 'should call the kill function on the user' do > byebug > expect(@user1).to receive(:kill) > put :update, id: :user_kill, user_id: @user1.id > end > … > > At the breakpoint in byebug, I set a breakpoint at "User#kill" and display > the value "@user1.id". When the program breaks at User#kill, I view “ > self.id” — same value. I then continue and the est fails with the > message: > > Failure/Error: expect(@user1).to receive(:kill) > (#<User:0x007fc38cd6a248>).kill(any args) > expected: 1 time with any arguments > received: 0 times with any arguments > > If I put another breakpoint after the “put” command, I can verify that the > record has been deleted (which is the effect of calling User#kill. I can > test the effect instead of testing the method call directly, but I’d rather > do it this way (I have several other functions whose effect is less easy to > detect, therefore I’d like to get this style of test to work). Any > thoughts? > > If it makes a difference, running RSpec 2.99 on Rails 3.2.21. (Please > don’t advise me to upgrade, I’m adding tests preparatory to upgrading.) -- You received this message because you are subscribed to the Google Groups "rspec" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/933f9086-36e0-4a5a-9123-a58087fcc6f0%40googlegroups.com.
