【问题标题】:Rspec undefined method 'to'Rspec未定义的方法'to'
【发布时间】:2014-09-09 12:03:57
【问题描述】:

所以我有一个未定义的方法错误,它适用于 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)>'

任何想法为什么会发生这种情况?

【问题讨论】:

    标签: ruby-on-rails rspec rspec-rails


    【解决方案1】:

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

     allow( FavoriteMailer )
    

    但看起来更好:

     allow(FavoriteMailer)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多