Wednesday 15 May 2013

ruby on rails - RSpec - Testing errors for 2 attributes that share the same validation(s) -



ruby on rails - RSpec - Testing errors for 2 attributes that share the same validation(s) -

i have 2 attributes within model share same validations

validates :first_name, :last_name, length: {minimum: 2}

right have :first_name attribute tested follows:

rspec.describe user, :type => :model 'is invalid if first name less 2 characters' user = user.create( first_name: 'a' ) expect(user).to have(1).errors_on(:first_name) end

for sake of developer isn't familiar how i've setup model wanted explicitly state 2 attributes' relationship this:

it 'is invalid if first name and/or lastly name has less 2 characters' user = user.create( first_name: 'a', last_name: 'b ) expect(user).to have(1).errors_on(:first_name, :last_name)

obviously throws error:

wrong number of arguments (2 0..1)

the same thing apply if had instituted 2 2 validations:

validates :first_name, :last_name, length: {minimum: 2}, format: {with: /^([^\d\w]|[-])*$/}

and seek test 2 errors:

it 'is invalid if first name and/or lastly name has less 2 characters , has special characters' user = user.create( first_name: '@', last_name: '# ) expect(user).to have(2).errors_on(:first_name, :last_name)

in rspec 3.x, can compound expectations .and:

it 'is invalid if first name and/or lastly name has less 2 characters' user = user.create(first_name: 'a', last_name: 'b') expect(user).to have(1).errors_on(:first_name).and have(1).errors_on(:last_name) end

check out rspec-expectations documentation more info.

for rspec 2.x, you'll need 1 of these:

it 'is invalid if first name and/or lastly name has less 2 characters' user = user.create(first_name: 'a', last_name: 'b') expect(user).to have(1).errors_on(:first_name) && have(1).errors_on(:last_name) end # or 'is invalid if first name and/or lastly name has less 2 characters' user = user.create(first_name: 'a', last_name: 'b') expect(user).to have(1).errors_on(:first_name) expect(user).to have(1).errors_on(:last_name) end

it's not pretty, should work.

edit:

op using rspec-collection_matchers gem. gem's custom matchers not include rspec 3 mixin module rspec::matchers::composable, #and method goes unrecognized.

there few things circumvent issue. easiest utilize && technique above (in rspec 2.x suggestions). utilize only rspec 3 matchers, need utilize be_valid:

it 'is invalid if first name and/or lastly name has less 2 characters' user = user.create(first_name: 'a', last_name: 'b') expect(user).to_not be_valid end

of course, not distinguish between first_name errors , last_name errors intended. be_valid matcher, you'd have break test 2 tests:

it 'is invalid if first name has less 2 characters' user = user.create(first_name: 'a', last_name: 'abc') expect(user).to_not be_valid end 'is invalid if lastly name has less 2 characters' user = user.create(first_name: 'abc', last_name: 'a') expect(user).to_not be_valid end

ruby-on-rails rspec

No comments:

Post a Comment