Skip to content

Commit

Permalink
extract assert_snapshot_unchanged_on_pass
Browse files Browse the repository at this point in the history
... to DRY up assert_snapshot_unchanged + run_as_test + assert_passed combo
  • Loading branch information
nitishr committed Jan 6, 2020
1 parent f3ddcac commit 1777b8c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
4 changes: 4 additions & 0 deletions test/acceptance/acceptance_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ def teardown_acceptance_test
Mocha::Configuration.reset_configuration
end

def assert_snapshot_unchanged_on_pass(object, &block)
assert_snapshot_unchanged(object) { assert_passed(run_as_test(&block)) }
end

include Introspection::Assertions
end
29 changes: 10 additions & 19 deletions test/acceptance/prepend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,28 @@ def my_method
end

def test_stubbing_any_instance_with_multiple_prepended_methods
assert_snapshot_unchanged(Klass1) do
test_result = run_as_test do
Klass1.any_instance.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', Klass1.new.my_method
end
assert_passed(test_result)
assert_snapshot_unchanged_on_pass(Klass1) do
Klass1.any_instance.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', Klass1.new.my_method
end
assert_equal 'Hello World Wide', Klass1.new.my_method
end

def test_stubbing_instance_with_multiple_prepended_methods
object = Klass1.new

assert_snapshot_unchanged(object) do
test_result = run_as_test do
object.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', object.my_method
assert_equal 'Hello World Wide', Klass1.new.my_method
end
assert_passed(test_result)
assert_snapshot_unchanged_on_pass(object) do
object.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', object.my_method
assert_equal 'Hello World Wide', Klass1.new.my_method
end
assert_equal 'Hello World Wide', object.my_method
end

def test_stubbing_a_prepended_class_method
assert_snapshot_unchanged(Klass2) do
test_result = run_as_test do
Klass2.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', Klass2.my_method
end
assert_passed(test_result)
assert_snapshot_unchanged_on_pass(Klass2) do
Klass2.stubs(:my_method).returns('Bye World')
assert_equal 'Bye World', Klass2.my_method
end
assert_equal 'Hello World Wide', Klass2.my_method
end
Expand Down
11 changes: 4 additions & 7 deletions test/acceptance/stub_method_shared_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ def assert_snapshot_unchanged_on_stubbing(visibility)
instance = stubbed_instance
stub_owner_in_scope = stub_owner || instance
method = alias_method? ? :my_aliased_method : stubbed_method_name
assert_snapshot_unchanged(instance) do
test_result = run_as_test do
stub_owner_in_scope.stubs(method).returns(:new_return_value)
assert_method_visibility instance, method, visibility
assert_equal :new_return_value, instance.send(method)
end
assert_passed(test_result)
assert_snapshot_unchanged_on_pass(instance) do
stub_owner_in_scope.stubs(method).returns(:new_return_value)
assert_method_visibility instance, method, visibility
assert_equal :new_return_value, instance.send(method)
end
assert_equal :original_return_value, instance.send(method)
end
Expand Down

0 comments on commit 1777b8c

Please sign in to comment.