这是我的规范文件:
require 'spec_helper'
describe User, "references" do
it { should have_and_belong_to_many(:roles) }
it { should belong_to(:account_type) }
it { should belong_to(:primary_sport).class_name("Sport") }
it { should belong_to(:school) }
it { should belong_to(:city) }
end
describe User, "factory" do
before(:each) do
@user = FactoryGirl.create(:user)
end
it "is invalid with no email" do
@user.email = nil
@user.should_not be_valid
end
it "is valid with email" do
@user.should be_valid
end
end
厂:
FactoryGirl.define do
factory :user do
email Faker::Internet.email
password "password"
password_confirmation "password"
agreed_to_age_requirements true
end
end
我试图“测试”的部分并且不确定如何100%检查以确保在创建用户时电子邮件地址不是零。
答案 0 :(得分:2)
shoulda提供验证助手,帮助您测试验证。
it { should validate_presence_of(:email) }
如果你想使用rspec并自己编写,那么
describe User do
it "should be invalid without email" do
user = FactoryGirl.build(:user, :email => nil)
@user.should_not be_valid
@user.errors.on(:email).should == 'can't be blank' #not sure about the exact message. But you will know when you run the test
end
it "should be valid with email" do
user = FactoryGirl.build(:user, :email => "user@user.com")
@user.should be_valid
end
end
运行测试时,它将显示为
User
should be invalid without email
should be valid with email
为您的测试用例提供一个很好的描述是非常重要的,因为它有点像文档。