前幾日摸了一下model的測(cè)試,不得其要點(diǎn),老是在寫測(cè)試不知如何下手,后閱遍大小論壇,總結(jié)一下測(cè)試方法呢,于是便模仿敏捷開(kāi)發(fā)一書中的關(guān)于登錄一章,寫了如下測(cè)試代碼
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寫的測(cè)試代碼
require 'spec_helper'
describe User do
it "正確參數(shù)" do
user = FactoryGirl.build(:user1)
assert user.valid?
end
it "name值為空時(shí),user對(duì)象應(yīng)該無(wú)效" do
user = FactoryGirl.build(:user2)
assert user.invalid?
end
it "password虛擬屬性為空時(shí),user對(duì)象應(yīng)該無(wú)效" do
user = Factory.build(:user5)
user.should_not be_valid
user.should have(1).errors_on(:password)
end
it "password_confirmation虛擬屬性為nil時(shí),user對(duì)象應(yīng)該無(wú)效" do
user = Factory.build(:user_password_confirmation_nil)
assert user.invalid?
user.should have(1).errors_on(:password)
end
it "兩次密碼不相同時(shí),user對(duì)象應(yīng)該無(wú)效" do
user = FactoryGirl.build(:user3)
assert user.invalid?
end
it "當(dāng)name已經(jīng)存在時(shí),對(duì)象不應(yīng)該被創(chuàng)建并會(huì)報(bào)關(guān)于:name的錯(cuò)誤" do
fist_user = Factory.create(:user1) #factory用create創(chuàng)建表示還有save動(dòng)作而用 build則沒(méi)有save
user = Factory.build(:user1)
assert user.invalid?
user.should have(1).errors_on(:name) #結(jié)果會(huì)有關(guān)于name一個(gè)錯(cuò)誤。
end
end
上面就是rspec測(cè)試代碼部分主要需要注意的是create與build創(chuàng)建時(shí)的不同
下面是factory構(gòu)建的測(cè)試代碼
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是虛擬屬性,在測(cè)試數(shù)據(jù)時(shí)要注意排除hashed_password salt屬性的干擾。
名稱欄目:學(xué)習(xí)札記——運(yùn)用Rspec+factory_girl進(jìn)行model測(cè)試
網(wǎng)址分享:http://aaarwkj.com/article12/gjdidc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、用戶體驗(yàn)、網(wǎng)站維護(hù)、網(wǎng)站策劃、ChatGPT、自適應(yīng)網(wǎng)站
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源:
創(chuàng)新互聯(lián)