@@ -112,17 +112,17 @@ class << self
112112 end
113113
114114 it 'checks with the converter when setting message types' do
115- converter . expects ( :convertible? ) . once . with ( nested_message_class ) . returns ( false )
115+ converter . expects ( :convertible? ) . at_least_once . with ( nested_message_class ) . returns ( false )
116116 resource = resource_class . new
117117 assert_raises ( ArgumentError ) do
118118 resource . nested_message = 5
119119 end
120120 end
121121
122122 it 'converts message types to and from their Ruby values when the converter allows' do
123- converter . expects ( :convertible? ) . times ( 2 ) . with ( nested_message_class ) . returns ( true )
123+ converter . expects ( :convertible? ) . at_least_once . with ( nested_message_class ) . returns ( true )
124124 converter . expects ( :to_message ) . once . with ( 6 , nested_message_class ) . returns ( nested_message_class . new number : 100 )
125- converter . expects ( :to_object ) . once . with ( nested_message_class . new number : 100 ) . returns 'intern'
125+ converter . expects ( :to_object ) . at_least_once . with ( nested_message_class . new number : 100 ) . returns 'intern'
126126
127127 resource = resource_class . new
128128 resource . nested_message = 6
@@ -434,13 +434,41 @@ def self.it_converts_query_parameters
434434 attrs = { id : 2 }
435435 assert_equal resource_message_class . new ( attrs ) , resource_class . new ( attrs ) . message
436436 end
437+ end
438+ end
437439
438- it 'delegates to #assign_attributes on its wrapper object when a hash is given ' do
439- attrs = { id : 3 }
440- Protip :: Wrapper . any_instance . expects ( :assign_attributes ) . once . with ( { id : 3 } )
441- resource_class . new ( attrs )
440+ describe 'attribute writer ' do
441+ before do
442+ resource_class . class_exec ( resource_message_class ) do | resource_message_class |
443+ resource actions : [ ] , message : resource_message_class
442444 end
443445 end
446+
447+ it 'delegates writes to the wrapper object' do
448+ resource = resource_class . new
449+ test_string = 'new'
450+ Protip ::Wrapper . any_instance . expects ( :string= ) . with ( test_string )
451+ resource . string = test_string
452+ end
453+
454+ it 'marks the resource and attribute as changed if the value is changed' do
455+ resource = resource_class . new string : 'original'
456+ resource . string = 'new'
457+ assert resource . changed? , 'resource should be marked as changed'
458+ assert resource . string_changed? , 'string field should be marked as changed'
459+ end
460+
461+ it 'does not mark the resource and attribute as changed if the value is not changed' do
462+ resource = resource_class . new string : 'original'
463+ resource . send :changes_applied # clear the changes
464+ # establish that the changes were cleared
465+ assert !resource . changed? , 'resource should be not marked as changed'
466+ assert !resource . string_changed? , 'string field should not be marked as changed'
467+
468+ resource . string = 'original'
469+ assert !resource . changed? , 'resource should be not marked as changed'
470+ assert !resource . string_changed? , 'string field should not be marked as changed'
471+ end
444472 end
445473
446474 describe '#assign_attributes' do
@@ -449,11 +477,17 @@ def self.it_converts_query_parameters
449477 resource actions : [ ] , message : resource_message_class
450478 end
451479 end
452- it 'delegates to #assign_attributes on its wrapper object' do
480+
481+ it 'calls the attribute writer for each attribute' do
453482 resource = resource_class . new
483+ test_string = 'whodunnit'
484+ resource . expects ( :string= ) . with ( test_string )
485+ resource . assign_attributes ( string : test_string )
486+ end
454487
455- Protip ::Wrapper . any_instance . expects ( :assign_attributes ) . once . with ( string : 'whodunnit' ) . returns ( 'boo' )
456- assert_equal 'boo' , resource . assign_attributes ( string : 'whodunnit' )
488+ it 'returns nil' do
489+ resource = resource_class . new
490+ assert_nil resource . assign_attributes ( string : 'asdf' )
457491 end
458492 end
459493
@@ -495,6 +529,14 @@ def self.it_converts_query_parameters
495529 resource . save
496530 assert_equal response , resource . message
497531 end
532+
533+ it 'marks changes as applied' do
534+ client . stubs ( :request ) . returns ( response )
535+ resource = resource_class . new ( string : 'time' )
536+ assert resource . string_changed? , 'string should initially be changed'
537+ assert resource . save
538+ assert !resource . string_changed? , 'string should no longer be changed after save'
539+ end
498540 end
499541
500542 describe 'for an existing record' do
@@ -528,6 +570,14 @@ def self.it_converts_query_parameters
528570 resource . save
529571 assert_equal response , resource . message
530572 end
573+
574+ it 'marks changes as applied' do
575+ client . stubs ( :request ) . returns ( response )
576+ resource = resource_class . new id : 5 , string : 'new_string'
577+ assert resource . string_changed? , 'string should initially be changed'
578+ assert resource . save
579+ assert !resource . string_changed? , 'string should no longer be changed after save'
580+ end
531581 end
532582
533583 describe 'when validation errors are thrown' do
@@ -567,6 +617,13 @@ def self.it_converts_query_parameters
567617 it 'returns false' do
568618 refute @resource . save , 'save returned true'
569619 end
620+
621+ it 'does not mark changes as applied' do
622+ @resource . string = 'new_string'
623+ assert @resource . string_changed? , 'string should initially be changed'
624+ refute @resource . save
625+ assert @resource . string_changed? , 'string should still be changed after unsuccessful save'
626+ end
570627 end
571628 end
572629
0 commit comments