Rspec:失败/错误:expect(分配(:用户))。be_persisted

时间:2015-10-07 12:21:07

标签: ruby-on-rails ruby rspec

我有一个简单的控制器Rspec测试。我正在测试用户控制器创建操作。

但我收到此错误

$ rspec spec/controllers/users_controller_spec.rb
.....F

Failures:

  1) UsersController POST #create user created
     Failure/Error: expect(assigns(:user)).to be_persisted
       expected `#<User id: nil, username: "username27", email: "user27@factory.com", created_at: nil, updated_at: nil, password_digest: nil, remember_digest: nil, admin: false, activation_digest: nil, activated: false, activated_at: nil, reset_digest: nil, >: nil, reset_sent_at: nil, avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil, description: nil, header_image_file_name: nil, header_image_content_type: nil, header_image_file_size: nil, header_image_updated_at: nil>.persisted?` to return true, got false
     # ./spec/controllers/users_controller_spec.rb:58:in `block (3 levels) in <top (required)>'

Finished in 0.49926 seconds (files took 2.6 seconds to load)

这是测试

require "rails_helper"

RSpec.describe UsersController, type: :controller do
  let!(:admin) { create(:user, password: "Password123", admin: true) }
  let!(:user) { create(:user, password: "Password456", admin: false) }

  let!(:users) do
    [admin, user] + 3.times.map { create(:user) }
  end

  let!(:user_params) { user.attributes }

  describe "POST #create" do
    before do
      post :create, user: user_params
    end

    it "user created" do
      expect(assigns(:user)).to be_persisted
    end
  end
end

当我在创建动作中放置binding.pry时。然后运行User.new(user_params).valid? = false

$ rspec spec/controllers/users_controller_spec.rb
.....
From: /Users/joseph/Documents/Safsy/Website/Safsy/Safsy/app/controllers/users_controller.rb @ line 26 UsersController#create:

    24: def create
    25:   @user = User.new(user_params)
 => 26:   binding.pry
    27:   if @user.save
    28:     @user.send_activation_email
    29:     flash[:info] = "Please check your email to activate your account."
    30:     redirect_to root_url
    31:   else
    32:     render 'new'
    33:   end
    34: end

[1] pry(#<UsersController>)> User.new(user_params)
=> #<User:0x007fa749fc1b18
 id: nil,
 username: "username27",
 email: "user27@factory.com",
 created_at: nil,
 updated_at: nil,
 password_digest: nil,
 remember_digest: nil,
 admin: false,
 activation_digest: nil,
 activated: false,
 activated_at: nil,
 reset_digest: nil,
 >: nil,
 reset_sent_at: nil,
 avatar_file_name: nil,
 avatar_content_type: nil,
 avatar_file_size: nil,
 avatar_updated_at: nil,
 description: "Vel aperiam et dolorem aliquid est perspiciatis.",
 header_image_file_name: nil,
 header_image_content_type: nil,
 header_image_file_size: nil,
 header_image_updated_at: nil>

任何线索如何解决?它就像我的测试中的user_params的一些属性一样也被传递给了创建动作

Here are is a binding.pry at the Test

    50:   end
    51:
    52:   describe "POST #create" do
    53:     before do
    54:       post :create, user: user_params
 => 55:       binding.pry
    56:     end
    57:
    58:     it "user created" do
    59:       expect(assigns(:user)).to be_persisted
    60:     end

[1] pry(#<RSpec::ExampleGroups::UsersController::POSTCreate>)> user_params
=> {"id"=>2,
 "username"=>"username27",
 "email"=>"user27@factory.com",
 "created_at"=>Wed, 07 Oct 2015 12:24:01 UTC +00:00,
 "updated_at"=>Wed, 07 Oct 2015 12:24:01 UTC +00:00,
 "password_digest"=>"$2a$04$Y627EwOml9cix7NdPITnpem0nZV8fta.JNyuezRsLNF2ucKfdC3GC",
 "remember_digest"=>nil,
 "admin"=>false,
 "activation_digest"=>"$2a$04$LHRBD82Lm0bj20nrwHDVveVlUa7zWq0WYIlmTMivT1h9UIcjwv9FO",
 "activated"=>true,
 "activated_at"=>nil,
 "reset_digest"=>nil,
 ">"=>nil,
 "reset_sent_at"=>nil,
 "avatar_file_name"=>nil,
 "avatar_content_type"=>nil,
 "avatar_file_size"=>nil,
 "avatar_updated_at"=>nil,
 "description"=>"Nihil eligendi ab debitis iure.",
 "header_image_file_name"=>nil,
 "header_image_content_type"=>nil,
 "header_image_file_size"=>nil,
 "header_image_updated_at"=>nil}

在测试中查看user_params,然后在create action中查看它们。他们是一样的。只有密码和密码确认等属性才能通过。

1 个答案:

答案 0 :(得分:1)

修改您的规格,如下所示,它应该有效 -

require "rails_helper"

RSpec.describe UsersController, type: :controller do
  let!(:admin) { create(:user, password: "Password123", admin: true) }
  let!(:user) { create(:user, password: "Password456", admin: false) }

  let!(:users) do
    [admin, user] + 3.times.map { create(:user) }
  end

  let!(:user_params) { user.dup.attributes }

  describe "POST #create" do
    it "user created" do
      valid_user_params = { username: "username#{rand(1000)}", 
       email: "user#{rand(1000)}@factory.com",
       password: "Password123",
       password_confirmation: "Password123",
       admin: false,
       description: "Nihil eligendi ab debitis iure." }
      post :create, user: valid_user_params
      expect(assigns(:user)).to be_persisted
    end
  end
end