spec/factories/users.rb
FactoryGirl.define do factory :user, :password, class: User do |f| sequence(f.login_name) { |n| "user_name_#{n}" } f.password_confirmation { |u| u.password } f.admin true end end
spec/models/user_spec.rb
require 'spec_helper' describe User do describe '#validate' do it "正常に保存できる" do @user = FactoryGirl.build(:user, password: 'murajun1978') expect(@user.save).to be true end context 'login_name' do before :all do @user = FactoryGirl.build(:user) end it "login_nameは数字のみは不可" do @user.login_name = "111" expect(@user.save).to be false end it "login_nameは英字のみは不可" do @user.login_name = "eee" expect(@user.save).to be false end it "login_nameの先頭は必ず英字" do @user.login_name = "111aaa" expect(@user.save).to be false end it "login_nameは必須" do @user.login_name = "" expect(@user.save).to be false end it "login_nameは重複不可" do @user = FactoryGirl.build(:user, login_name: 'user_name_1') expect(@user.save).to be false end end after :all do User.delete_all end end end
Factory_Girlを少しシンプルにしてみましたヘ(^o^)ノ
login_nameは連番(user_name_1..user_name_2.. みたいな)
いい感じになったかな?
( ̄(エ) ̄)彡☆
0 件のコメント:
コメントを投稿