ROR - rails - rspec - factory_girl should_not not working (as expected) -
the thing i'm expecting 2-nd it
fail , 3-rd pass, opposite going on. 2 passing , 3 failing. someon know why ?
spec/models/user_spec.rb
require 'spec_helper' describe user 'has valid factory' factorygirl.create(:user).should be_valid end 'is invalid empty email1' # i've tried # factorygirl.build(:user, email: nil, validation_scenario: 'create').should be_valid factorygirl.build(:user, email: nil).should be_valid end 'is invalid empty email2' factorygirl.build(:user, email: nil).should_not be_valid end end
spec/factories/user.rb
require 'faker' factorygirl.define factory :user |u| u.email { faker::internet.email } u.password 'password' u.password_confirmation 'password' end end
models/user.rb
class user < activerecord::base attr_accessor :password, :validation_scenario # validate on user create/update with_options if: -> user { ['create', 'update'].include? user.validation_scenario } |user| user.validates_confirmation_of :password user.validates_presence_of :password, :on => :before_create user.validates_presence_of :email, :password user.validates_uniqueness_of :email user.validates_format_of :email, :with =>/.+@.+\..+/i end # validate on resend email cofirmation/reset password with_options if: -> user { ['send_email_confirmation', 'reset_password'].include? user.validation_scenario } |user| user.validates_presence_of :email user.validates_format_of :email, :with =>/.+@.+\..+/i end ...
controllers/user_controller.rb
class userscontroller < applicationcontroller def create @user = user.new(user_params) @user.validation_scenario = 'create' respond_to |format| if @user.save format.html { redirect_to root_url, notice: 'user created.' } else format.html { render action: 'new' } end end end def user_params params.require(:user).permit(:email, :password, :password_confirmation) end end
i'm following tutorial here: http://everydayrails.com/2012/03/19/testing-series-rspec-models-factory-girl.html
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
rails 4.0.2
the thing was not setting validation_scenario
anywhere. now:
spec/factories/user.rb looks this:
require 'faker' factorygirl.define factory :user |u| u.email { faker::internet.email } u.password 'password' u.password_confirmation 'password' end factory :create_user, parent: :user |u| u.validation_scenario 'create' end end
and spec/model/user_spec.rb
require 'spec_helper' describe user 'has valid factory' factorygirl.create(:create_user).should be_valid end 'is invalid empty email' factorygirl.build(:create_user, email: nil).should_not be_valid end 'is invalid empty password' factorygirl.build(:create_user, password: nil).should_not be_valid end 'is invalid empty password_confirmation' factorygirl.build(:create_user, password: nil).should_not be_valid end end
works charm
Comments
Post a Comment