[英]Ruby on Rails Rspec NoMethodError: undefined method 'not_to'
我正在使用Ruby on Rails构建的基本应用程序中测试电子邮件功能。 对于使用.not_to的任何测试,我都会收到NoMethodError。 这是我遵循的教程提供的Rspec:
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
这是错误:
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
我怀疑这些行上的代码是以与预期不同的顺序解释的:
expect ( FavoriteMailer )
.not_to receive(:new_comment)
尝试这个:
expect( FavoriteMailer ).not_to receive(:new_comment)
需要注意的主要一点是,在Ruby中的一个好习惯是在方法调用和参数的左括号之间不留空格(即使这通常可以奏效,但最好避免停止这样的麻烦)。
在此示例中,这意味着删除expect
和( FavoriteMailer )
之间的空间。
不是正确的方法to_not而不是not_to吗?
更新更改为to_not和not_to。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.