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