Search code examples
ruby-on-railsrspecrspec-rails

Rspec undefined method 'to'


So I have an undefined method error which is for the Rspec method 'to'. I have a spec with the following code

  it "sends an email to users who have favorited the post" do
    @user.favorites.where(post: @post).create

    allow ( FavoriteMailer )
      .to receive(:new_comment)
      .with(@user, @post, @comment)
      .and_return( double(deliver: true))

When I run the spec I get the following error:

1) Comment after_create with users permission sends an email to users who have favorited the post
 Failure/Error: allow ( FavoriteMailer )
 NoMethodError:
   undefined method `to' for FavoriteMailer:Class
 # ./spec/models/comment_spec.rb:18:in `block (4 levels) in <top (required)>'

Any ideas why this is happening?


Solution

  • just remove space before first bracket, so you could write:

     allow( FavoriteMailer )
    

    but better it looks like:

     allow(FavoriteMailer)
    

    in your case: allow (FavoriteMailer).to .. is interpreted as: allow((FavoriteMailer).to ..)