Should native validations be tested in rails?

前端 未结 5 2692
醉梦人生
醉梦人生 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:10

    Yes.

    Testing that a model attribute is present or not is only testing the validates_presence_of code as a by-product of the real test which is that the validates_presence_of exists within your model.

    If someone commented out a bunch of validation code and then forgot to uncomment it then this would go undetected and could cause all sorts of problems.

    I test them, not because I think they don't work but to ensure that they are present in my model when required.

    0 讨论(0)
  • 2021-02-20 08:14

    Are you planning to write unit tests for every single Ruby operator and API method as well?

    Your unit tests should test your own code, not other people's - that's their job, why duplicate their work? And if you don't trust them to do their job well, why are you using their code?

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-20 08:15

    This is where tools like Shoulda really come in handy. I think its totally up to you to test how you write the code with the tools people provide you. Just because you are using has_many, does not mean you are using it right!

    Seriously, if you integrate Shoulda into the mix, it becomes trivial to test these sorts of things. if you run rcov, its going to tell you all the code you wrote is not fully tested, unless you do.

    0 讨论(0)
  • 2021-02-20 08:16

    Matthew Bass has a great gem he's released for just this type of thing. It adds rspec matchers that check to make sure the validation is in place without actually running the underlying ActiveRecord code. Read more about it here.

    It adds matchers for validations:

    it_should_validate_presence_of     :first_name, :last_name, :email
    it_should_validate_numericality_of :zip
    it_should_validate_uniqueness_of   :email
    it_should_validate_inclusion_of    :gender, :in => %w(Male Female)
    

    Also matchers for associations:

    it_should_belong_to :employer
    it_should_have_many :friends, :romans, :countrymen
    it_should_have_one  :account
    it_should_have_and_belong_to_many :comments
    

    And a few other useful additions:

    # tests that User.count increases by 1
    it_should_be_createable :with => {:first_name => 'Ed', :last_name => 'The Duck', :email => 'a@b.com'}
    
    # tests that the attribute is protected
    it_should_protect :email
    

    That's not by any means an exhaustive list. I've got a fork where I've added a few others I needed, likely there are others floating around as well. It's a good approach and for me fit the middle ground between ensuring the validations were still in the model, and having to explicitly write tests to execute ActiveRecord code to ensure it.

    0 讨论(0)
提交回复
热议问题