【问题标题】:Why is this test passing when it shouldnt'?为什么这个测试在不应该通过的时候通过了?
【发布时间】:2013-09-05 03:26:40
【问题描述】:

所以我有以下测试:

it "should not update a user based on invalid info" do
    put :update, :id => @factory.id, :user => {
       :name => '', :user_name => '',
       :email => '', :email_confirmation => '',
       :password => '', :password_confirmation => 'dfgdfgdfg',
       :bio => '', :picture_url => ''
    }
end   

显然有缺失的数据。

然后我有以下控制器:

  def update
    @user = User.friendly.find(params[:id])
    @user.update_attributes(user_update_params)
    if @user.save
      render :show
    else
      render :edit
    end
  end

这有以下私有方法:

  def user_update_params
    params.require(:user).permit(:name, :user_name, :email, :email_confirmation, :password,
      :password_confirmation, :bio, :picture_url)
  end  

当这个测试运行时它通过了 - 它应该给我一个ActiveRecord::RecordInvalid

如果您对此模型感兴趣:

class User < ActiveRecord::Base
  attr_accessor :password

  before_save :encrypt_password

  validates :name, uniqueness: true, presence: true
  validates :user_name, uniqueness: true, presence: true, length: {minimum: 5}
  validates :email, presence: true, confirmation: true, uniqueness: true, email_format: {message: "what is this? it's not an email"}
  validates :password, presence: true, confirmation: true, length: {minimum: 10}

  extend FriendlyId
  friendly_id :name, use: [:slugged, :history]

  def self.authenticate(user_name, password)
    user = User.find_by(user_name: user_name)
    if(user && user.password_hash == BCrypt::Engine.hash_secret(password, user.salt))
      user
    else
      nil
    end
  end

  def encrypt_password
    if password.present?
      self.salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, salt)
    end
  end
end

我也打赌这是一件非常微不足道的事情

更新如果您感兴趣,这是我的工厂:

FactoryGirl.define do
  factory :user, :class => 'User' do
    name "sample_user"
    email "MyString@gmail.com"
    user_name "MyString"
    password "someSimpleP{ass}"
  end
end

所以@factory 是从@factory = FactoryGirl.create(:user) 创建的

【问题讨论】:

  • 测试的期望部分是什么?您发布的代码缺少该部分。例如response.should_not be_valid
  • 看来您错过了预期。可能,expect(response).to render_template('edit') 因为它失败了。

标签: ruby-on-rails activerecord rspec


【解决方案1】:

您正在执行一个 RSpec 方法 (put),只要参数格式正确,就不会引发错误,以便可以将消息发送到服务器。由于您的论点本身没有问题,因此没有引发错误。 服务器 无法成功完成请求将反映在响应中,您需要单独测试。

当然,正如其他人所指出的,在 RSpec 示例中,通常会在代码上设置“期望”,这将确定示例是否成功,因此不仅仅是没有未捕获的错误将决定成功.

【讨论】:

    【解决方案2】:

    不是说测试不通过,而是没有测试。你错过了测试中的期望。 试试这样的。

    it "should not update a user based on invalid info" do
        put :update, :id => @factory.id, :user => {
           :name => '', :user_name => '',
           :email => '', :email_confirmation => '',
           :password => '', :password_confirmation => 'dfgdfgdfg',
           :bio => '', :picture_url => ''
        }
        #add expectation here
        response.should_not be_valid
    end 
    

    任何没有期望的测试都会通过。

    【讨论】:

    • 虽然这是真的,但 OP 在“不引发错误”的意义上意味着“通过”。特别是,OP 询问为什么没有提出 ActiveRecord::RecordInvalid
    • 没有什么会引发该异常。 update_attributes 将返回 true 或 false。
    • 您的代码在有效时抛出无方法错误?我用 expect_response().to render()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多