Rspec未定义方法'到'

时间:2014-09-09 12:03:58

标签: 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)
      .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 个答案:

答案 0 :(得分:2)

在第一个括号之前删除空格,这样你就可以写:

 allow( FavoriteMailer )

但看起来更好:

 allow(FavoriteMailer)

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