Should native validations be tested in rails?

前端 未结 5 2714
醉梦人生
醉梦人生 2021-02-20 07:25

Everybody knows that automated testing is a good thing.

Not everybody knows exacly what to test.

My question is if native validations like validate_presence_of,

5条回答
  •  名媛妹妹
    2021-02-20 08:14

    Test the code you write. ActiveRecord has great test coverage including coverage for the validation class methods.

    Spec:

    require 'spec_helper'
    
    describe User do
      before(:each) do
        @user = User.new
      end
    
      it "should not be valid without an email" do
        @user.save.should be_false
        @user.should_not be_valid
        @user.email = "example@example.com"
        @user.should be_valid
        @user.save.should be_true
      end
    
    end
    

    To get that spec to pass you would need

    class User < ActiveRecord::Base
      validates_presence_of :email
    end
    

提交回复
热议问题