简体   繁体   中英

Rspec - Testing an instance of a method

I'm implemented a condition that if a user's email is not confirmed, they do not receive emails. My logic is, if the email isn't verified/ confirmed, return an instance of ActionMailer::Base::NullMail.new .

I wrote this test

 if @user.verified?
  next
 else 
  ActionMailer::Base::NullMail.new
 end 

 expect(TestMailer.test_mail(expired_account)).to be_an_instance_of(ActionMailer::Base::NullMail)

and the error I'm getting is

expected #<ActionMailer::MessageDelivery(#<ActionMailer::Base::NullMail:0x00007fb0f2f24090>)> to be a kind of ActionMailer::Base::NullMail

The question is, how can I re-write my test so that I can test that if the emails aren't verified they should return an instance of ActionMailer::Base::NullMail.new

The error is telling you what the problem is right? You expect an instance of a mail type object, so change it to this. Also you should be able to use be_a method in place of be_an_instance_of in Rspec see docs

expect(TestMailer.test_mail(expired_account).message)
  .to be_an(ActionMailer::Base::NullMail)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM