为什么我的脚手架生成的代码没有通过脚手架生成的Rspec测试?

时间:2013-11-19 16:58:45

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

这是rspec测试:

describe "POST create" do
  describe "with valid params" do
    it "creates a new User" do
      expect {
        post :create, {:user => valid_attributes}, valid_session
      }.to change(User, :count).by(1)
    end

    it "assigns a newly created user as @user" do
      post :create, {:user => valid_attributes}, valid_session
      assigns(:user).should be_a(User)
      assigns(:user).should be_persisted
    end

    it "redirects to the created user" do
      post :create, {:user => valid_attributes}, valid_session
      response.should redirect_to(User.last)
    end
  end
end

这是控制器代码:

def create
  @user = User.new(user_params)

  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render action: 'show', status: :created, location: @user }
    else
      format.html { render action: 'new' }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

这是我的失败:

  1) UsersController POST create with valid params creates a new User
 Failure/Error: expect {
   count should have been changed by 1, but was changed by 0
 # ./spec/controllers/users_controller_spec.rb:68:in `block (4 levels) in <top (required)>'

  2) UsersController POST create with valid params assigns a newly created user as @user
 Failure/Error: assigns(:user).should be_persisted
   expected persisted? to return true, got false
 # ./spec/controllers/users_controller_spec.rb:77:in `block (4 levels) in <top (required)>'

  3) UsersController POST create with valid params redirects to the created user
 Failure/Error: response.should redirect_to(User.last)
   Expected response to be a <redirect>, but was <200>
 # ./spec/controllers/users_controller_spec.rb:82:in `block (4 levels) in <top (required)>'

  4) UsersController PUT update with valid params redirects to the user
 Failure/Error: response.should redirect_to(user)
   Expected response to be a <redirect>, but was <200>
 # ./spec/controllers/users_controller_spec.rb:124:in `block (4 levels) in <top (required)>'

这感觉就像配置问题,因为一切看起来都对我不对。但我可能是错的。

更新: 来自users_controller.rb

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:title, :first_name, :last_name, :email, :password_digest, :admin)
    end

来自规范:

let(:valid_attributes) { { title: "MyString", first_name: "Example", last_name: "User", email: "my@address.com", password: "password", password_confirmation: "password" } }

let(:valid_session) { {} }

和我的users.rb:

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  validates :first_name, presence: true, length: { maximum: 50 }
  validates :last_name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                format:     { with: VALID_EMAIL_REGEX },
                uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }

  has_many :users_to_groups, :dependent => :destroy
  has_many :groups, :through => :users_to_groups
end

1 个答案:

答案 0 :(得分:2)

问题在于我的user_params。它应该是:password:password_confirmation(不是:password_digest

def user_params
  params.require(:user).permit(:title, :first_name, :last_name, :email, :password, :password_confirmation, :admin)
end