Ruby on rails Rspec未定义方法';至';

Ruby on rails Rspec未定义方法';至';,ruby-on-rails,rspec,rspec-rails,Ruby On Rails,Rspec,Rspec Rails,所以我有一个未定义的方法错误,这是Rspec方法“to”的错误。我有一个具有以下代码的规范 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

所以我有一个未定义的方法错误,这是Rspec方法“to”的错误。我有一个具有以下代码的规范

  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))
当我运行规范时,出现以下错误:

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)>'
1)使用用户权限创建后的评论将向喜欢该帖子的用户发送电子邮件
失败/错误:允许(FavoriteMailer)
命名错误:
FavoriteMailer:类的未定义方法“to”
#./spec/models/comment_spec.rb:18:in'block(4层)in'

知道为什么会发生这种情况吗?

只需删除第一个括号前的空格,就可以写:

 allow( FavoriteMailer )
但看起来更好的是:

 allow(FavoriteMailer)
在您的情况下:
allow(FavoriteMailer).to..
被解释为:
allow((FavoriteMailer).to..)