Jack Pope
Jack Pope

Reputation: 164

Ruby on Rails Rspec NoMethodError: undefined method 'not_to'

I am testing an email function in a basic app I am building with Ruby on Rails. I get a NoMethodError for any test where I use .not_to. Here is the Rspec, provided by the tutorial I am following:

require 'rails_helper'

describe Comment do
  include TestFactories

  describe "after_create" do

    before do
      @post = associated_post
      @user = authenticated_user
      @comment = Comment.new(body: "My comment", post: @post, user_id: 10000)
    end

    context "with user's permission" do

      it "send 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) )

        @comment.save
      end

      it "does not send emails to users who haven't" do
        expect ( FavoriteMailer )
          .not_to receive(:new_comment)

        @comment.save
      end
    end

    context "without permission" do
      before { @user.update_attribute(:email_favorites, false) }

      it "does not send emails, even to users who have favorited" do
        @user.favorites.where(post: @post).create

        expect ( FavoriteMailer )
          .not_to receive(:new_comment)

        @comment.save
      end
    end

  end
end

And here is the error:

Failures:

  1) Comment after_create with user's permission does not send emails to users who haven't
     Failure/Error: expect ( FavoriteMailer )
     NoMethodError:
       undefined method `not_to' for FavoriteMailer:Class
     # /home/vagrant/.rvm/gems/ruby-2.0.0-p576/gems/actionmailer-4.0.10/lib/action_mailer/base.rb:482:in `method_missing'
     # ./spec/models/comment_spec.rb:28:in `block (4 levels) in <top (required)>'

  2) Comment after_create without permission does not send emails, even to users who have favorited
     Failure/Error: expect ( FavoriteMailer )
     NoMethodError:
       undefined method `not_to' for FavoriteMailer:Class
     # /home/vagrant/.rvm/gems/ruby-2.0.0-p576/gems/actionmailer-4.0.10/lib/action_mailer/base.rb:482:in `method_missing'
     # ./spec/models/comment_spec.rb:41:in `block (4 levels) in <top (required)>'

Finished in 4.66 seconds (files took 24.42 seconds to load)
3 examples, 2 failures

Failed examples:

rspec ./spec/models/comment_spec.rb:27 # Comment after_create with user's permission does not send emails to users who haven't
rspec ./spec/models/comment_spec.rb:38 # Comment after_create without permission does not send emails, even to users who have favorited

Upvotes: 1

Views: 3184

Answers (2)

Eliot Sykes
Eliot Sykes

Reputation: 10073

I suspect the code on these lines is being interpreted in a different order to what's intended:

expect ( FavoriteMailer )
      .not_to receive(:new_comment)

Try this:

expect( FavoriteMailer ).not_to receive(:new_comment)

The main thing to note is it's a good convention in Ruby to not leave a space between a method call and the opening bracket for the arguments (even though this can often work, it's best avoided to stop head-scratchers like this cropping up).

In this example, that means remove the space between expect and ( FavoriteMailer ).

Upvotes: 2

Roope Hakulinen
Roope Hakulinen

Reputation: 7405

Isn't the correct method to_not instead of not_to?

update Changed to_not and not_to other way around.

Upvotes: 0

Related Questions