ruby on rails - Rspec testing validation -


i new rails , i'm trying understand testing using rspec. created simple model (just testing purposes) named contact contains firstname:string, lastname:string , email:string. trying set test firstname fail if empty.

my rspec test follow:

describe contact     "creates valid model"          contact = contact.new(             firstname: 'firstname',             lastname: 'lastname',             email: 'email'             )         expect(contact).to be_valid      end   "is invalid without firstname"          contact = contact.new(firstname: nil)          contact.valid?         expect(contact.errors[:firstname]).to include("can't blank")      end 

my understanding test should not return failures does. output below:

failures:    1) contact invalid without firstname      failure/error: expect(contact.errors[:firstname]).to include("can't blank")        expected [] include "can't blank"      # ./spec/models/contact_spec.rb:16:in `block (2 levels) in <top (required)>'  finished in 0.11659 seconds (files took 2.39 seconds load) 18 examples, 1 failure, 15 pending  failed examples:  rspec ./spec/models/contact_spec.rb:13 # contact invalid without firstname 

if change expect statement ".to" "not_to" test passes, think getting backwards, or explanation appreciated.

as smathy said in comments, forgot include validation inside model. solved problem.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -