Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么rspec错误通过?_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 为什么rspec错误通过?

Ruby on rails 为什么rspec错误通过?,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我正在做一个项目,学习如何将rspec与rubyonrails结合使用 因此,我编写了一个规范来测试我的应用程序是否在用户授予接收电子邮件的权限时发送新评论,而在用户未授予权限时不发送电子邮件 ###COMMENT SPEC### require 'rails_helper' describe Comment do include TestFactories describe "after_create" do before do @post = associa

我正在做一个项目,学习如何将rspec与rubyonrails结合使用

因此,我编写了一个规范来测试我的应用程序是否在用户授予接收电子邮件的权限时发送新评论,而在用户未授予权限时不发送电子邮件

###COMMENT SPEC###
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: 1000)
    end

    context "with user's permission" do

      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) )

        @comment.save  ### Test fails if this changes to @comment.save!
      end

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

        @comment.save  ### Test fails if this changes to @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  ### Test fails if this changes to @comment.save!
      end
    end
  end
end
这是我的测试工厂

module TestFactories

  def associated_post(options={})
    post_options = {
      title: 'Post title',
      body: 'Post bodies must be pretty long.',
      topic: Topic.create(name: 'Topic name'),
      user: authenticated_user
    }.merge(options)

    Post.create(post_options)
  end

  def authenticated_user(options={})
    user_options = {email: "email#{rand}@fake.com", password: 'password'}.merge(options)
    user = User.new(user_options)
    user.skip_confirmation!
    user.save
    user
  end
end
现在如果我将@comment.save更改为@comment.save!对于规范中的测试,测试失败,我得到以下错误:

Comment after_create without permission does not send emails, even to users who have favorited
Failure/Error: @comment.save!
ActiveRecord::RecordInvalid:
Validation failed: User can't be blank
我的工厂有缺陷还是这个测试有缺陷? 我对到底出了什么问题感到困惑


谢谢

user\u id:1000
-没有这样的用户。而且你已经对它进行了状态验证。啊,那么通过使用comment.save!,我故意破坏测试,因为我无法保存与不存在的用户相关的评论。是的。
将引发错误。没有
@comment.save
仅返回false,注释无法以静默方式保存。