ruby on rails - Understand Test-Driven Development with Rspec and FactoryGirl -


here spec file:

require 'spec_helper'  describe user, "references"   { should have_and_belong_to_many(:roles) }   { should belong_to(:account_type) }   { should belong_to(:primary_sport).class_name("sport") }   { should belong_to(:school) }   { should belong_to(:city) } end   describe user, "factory"   before(:each)     @user = factorygirl.create(:user)   end    "is invalid no email"     @user.email = nil     @user.should_not be_valid   end    "is valid email"     @user.should be_valid   end end 

factory:

factorygirl.define   factory :user     email faker::internet.email     password "password"     password_confirmation "password"     agreed_to_age_requirements true   end  end 

the part trying "test" , not sure how 100% checking make sure when user created email address not nil.

shoulda provides validation helpers test validations.

it { should validate_presence_of(:email) } 

if want use rspec , write own, then

describe user   "should invalid without email"     user = factorygirl.build(:user, :email => nil)     @user.should_not be_valid     @user.errors.on(:email).should == 'can't blank' #not sure exact message. know when run test   end    "should valid email"     user = factorygirl.build(:user, :email => "user@user.com")     @user.should be_valid   end end 

when run test, read as

user   should invalid without email   should valid email 

giving description test case important, because kind of acts documentation.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -