ruby on rails - RSpec: Expect to change multiple -


i want check many changes in model when submitting form in feature spec. example, want make sure user name changed x y, , encrypted password changed value.

i know there questions already, didn't find fitting answer me. accurate answer seems changemultiple matcher michael johnston here: is possible rspec expect change in 2 tables?. downside 1 check explicit changes known values known values.

i created pseudo code on how think better matcher like:

expect {   click_button 'save' }.to change_multiple { @user.reload }.with_expectations(   name:               {from: 'donald', to: 'gustav'},   updated_at:         {by: 4},   great_field:        {by_at_leaset: 23},   encrypted_password: true,  # must change   created_at:         false, # must not change   some_other_field:   nil    # doesn't matter, want denote here field exists ) 

i have created basic skeleton of changemultiple matcher this:

module rspec   module matchers     def change_multiple(receiver=nil, message=nil, &block)       builtin::changemultiple.new(receiver, message, &block)     end      module builtin       class changemultiple < change         def with_expectations(expectations)           # here? how add expectations passed argument?         end       end     end   end end 

but i'm getting error:

 failure/error: expect {    must pass argument rather block use provided matcher (nil), or matcher must implement `supports_block_expectations?`.  # ./spec/features/user/registration/edit_spec.rb:20:in `block (2 levels) in <top (required)>'  # /users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'  # /users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load' 

any in creating custom matcher highly appreciated.

in rspec 3 can setup multiple conditions @ once (so single expectation rule not broken). sth like:

expect {   click_button 'save'   @user.reload }.to change { @user.name }.from('donald').to('gustav')  .and change { @user.updated_at }.by(4)  .and change { @user.great_field }.by_at_least(23}  .and change { @user.encrypted_password } 

it not complete solution though - far research went there no easy way and_not yet. unsure last check (id doesn't matter, why test it?). naturally should able wrap within custom matcher. https://www.relishapp.com/rspec/rspec-expectations/v/2-4/docs/custom-matchers/define-matcher


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -