溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

學(xué)習(xí)札記——運(yùn)用Rspec+factory_girl進(jìn)行model測試

發(fā)布時(shí)間:2020-07-08 07:43:07 來源:網(wǎng)絡(luò) 閱讀:371 作者:JackSongBlack 欄目:編程語言
前幾日摸了一下model的測試,不得其要點(diǎn),老是在寫測試不知如何下手,后閱遍大小論壇,總結(jié)一下測試方法呢,于是便模仿敏捷開發(fā)一書中的關(guān)于登錄一章,寫了如下測試代碼
model代碼
require "digest/sha2"
class User < ActiveRecord::Base
    attr_accessible :name,:password,:password_confirmation
    validates :name,:presence =>true,:uniqueness => true

    validates :password,:confirmation =>true
    attr_accessor :password_confirmation
    attr_reader :password

    validate :password_must_be_present

    def password=(password)
        @password = password
        if password.present?
            generate_salt
            self.hashed_password = self.class.encrypt_password(password,salt)
        end
    end

    def User.authenticate(name,password)
        if user == find_by_name(name)
            if user.hashed_password == encrypt_password(password,user.salt)
                user
            end
        end
    end

    private
     def password_must_be_present
            errors.add(:password,"密碼錯(cuò)誤")unless hashed_password.present?
     end

     def User.encrypt_password(password,salt)
             Digest::SHA2.hexdigest(password + "wibble" + salt)
     end

    def generate_salt
        self.salt= self.object_id.to_s + rand.to_s
    end
end
這是模型部分代碼,他的需求如下
1.name與password不得為nil
2.兩次密碼輸入必須一致
3.name是唯一的
4..hashed_password與self不得暴露給用戶
下面就是rspec寫的測試代碼
require 'spec_helper'

describe User do

    it "正確參數(shù)" do
        user = FactoryGirl.build(:user1)
        assert user.valid?
    end

    it "name值為空時(shí),user對象應(yīng)該無效" do
        user = FactoryGirl.build(:user2)
        assert user.invalid?
    end

    it "password虛擬屬性為空時(shí),user對象應(yīng)該無效" do
        user = Factory.build(:user5)
        user.should_not be_valid
        user.should have(1).errors_on(:password)
    end

    it "password_confirmation虛擬屬性為nil時(shí),user對象應(yīng)該無效" do
        user = Factory.build(:user_password_confirmation_nil)
        assert user.invalid?
        user.should have(1).errors_on(:password)
    end

    it "兩次密碼不相同時(shí),user對象應(yīng)該無效" do
        user = FactoryGirl.build(:user3)
        assert user.invalid?
    end

    it "當(dāng)name已經(jīng)存在時(shí),對象不應(yīng)該被創(chuàng)建并會報(bào)關(guān)于:name的錯(cuò)誤" do
        fist_user = Factory.create(:user1) #factory用create創(chuàng)建表示還有save動(dòng)作而用 build則沒有save
        user = Factory.build(:user1)
        assert user.invalid?
        user.should have(1).errors_on(:name)  #結(jié)果會有關(guān)于name一個(gè)錯(cuò)誤。
    end

end

上面就是rspec測試代碼部分主要需要注意的是create與build創(chuàng)建時(shí)的不同
下面是factory構(gòu)建的測試代碼

FactoryGirl.define do
    factory :user1 ,:class =>:User do
        name "user1"
        hashed_password "MyString"
        salt "MyString"
        password "123"
        password_confirmation "123"
    end

    factory :user2 ,:class =>:User do
        name ""
        hashed_password "MyString"
        salt "MyString"
        password "123"
        password_confirmation "123"
    end

    factory :user3 ,:class =>:User do
        name "user3"
        hashed_password "MyString"
        salt "MyString"
        password "123"
        password_confirmation "1234"
    end

    factory :user4 ,:class =>:User do
        name "user4"
        hashed_password "MyString"
        salt "MyString"
    end
    factory :user5 ,:class =>:User do
        name "user1"
        password ""
        password_confirmation ""
    end
    factory :user_password_confirmation_nil ,:class =>:User do
        name"user"
        password "123456"
        password_confirmation ""
    end


end

 

需要注意的是  password_confirmation和  password是虛擬屬性,在測試數(shù)據(jù)時(shí)要注意排除hashed_password salt屬性的干擾。
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI