mocktail 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/CHANGELOG.md +4 -0
 - data/Gemfile.lock +1 -1
 - data/README.md +53 -1
 - data/bin/rake +29 -0
 - data/lib/mocktail/collects_calls.rb +13 -0
 - data/lib/mocktail/version.rb +1 -1
 - data/lib/mocktail.rb +8 -0
 - metadata +5 -3
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 0c303d4fb9ecc3a03e30fcfb52425cec2c4f3b2ca2ad3dadda3f9a09c1b8785a
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 90b573a3b6417d1c084fa7ced22af80b0533e36c45240d42c9627b3bfde2a032
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: f2aa7140202bfb3d5cadc45964f344cce19ba688d807b9c25b2033d27dd15710424fc00315633129429215892ea149160d2fbba2a8671bb2237867b12ce7c225
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 95f1c02d8f23849f0a5932e8cddb3f811dc01962e13e0553f1964ebf9f77489b49ce375506e3f2a5f9132bcda7493b2ffc8a03dc61dd4f5018f84fe33995245f
         
     | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | 
         @@ -12,7 +12,7 @@ and even safely override class methods. 
     | 
|
| 
       12 
12 
     | 
    
         | 
| 
       13 
13 
     | 
    
         
             
            If you'd prefer a voice & video introduction to Mocktail aside from this README,
         
     | 
| 
       14 
14 
     | 
    
         
             
            you might enjoy this ⚡️[Lightning
         
     | 
| 
       15 
     | 
    
         
            -
            Talk](https://blog.testdouble.com/talks/2022-05-18-please-mock-me?utm_source=twitter&utm_medium=organic-social&utm_campaign=conf-talk)⚡️ 
     | 
| 
      
 15 
     | 
    
         
            +
            Talk](https://blog.testdouble.com/talks/2022-05-18-please-mock-me?utm_source=twitter&utm_medium=organic-social&utm_campaign=conf-talk)⚡️
         
     | 
| 
       16 
16 
     | 
    
         
             
            from RailsConf 2022.
         
     | 
| 
       17 
17 
     | 
    
         | 
| 
       18 
18 
     | 
    
         
             
            ## An aperitif
         
     | 
| 
         @@ -404,6 +404,10 @@ verify { |m| auditor.record!("ok", user_id: 1) { |block| block.call.is_a?(Date) 
     | 
|
| 
       404 
404 
     | 
    
         
             
            * `ignore_block` will similarly allow the demonstration to forego specifying a
         
     | 
| 
       405 
405 
     | 
    
         
             
              block, even if the actual call receives one
         
     | 
| 
       406 
406 
     | 
    
         | 
| 
      
 407 
     | 
    
         
            +
            Note that if you want to verify a method _wasn't_ called at all or called a
         
     | 
| 
      
 408 
     | 
    
         
            +
            specific number of times—especially if you don't care about the parameters, you
         
     | 
| 
      
 409 
     | 
    
         
            +
            may want to look at the [Mocktail.calls()](#mocktailcalls) API.
         
     | 
| 
      
 410 
     | 
    
         
            +
             
     | 
| 
       407 
411 
     | 
    
         
             
            ### Mocktail.matchers
         
     | 
| 
       408 
412 
     | 
    
         | 
| 
       409 
413 
     | 
    
         
             
            You'll probably never need to call `Mocktail.matchers` directly, because it's
         
     | 
| 
         @@ -807,6 +811,54 @@ The `reference` object will have details of the `call` itself, an array of 
     | 
|
| 
       807 
811 
     | 
    
         
             
            `other_stubbings` defined on the faked method, and a `backtrace` to determine
         
     | 
| 
       808 
812 
     | 
    
         
             
            which call site produced the unexpected `nil` value.
         
     | 
| 
       809 
813 
     | 
    
         | 
| 
      
 814 
     | 
    
         
            +
            ### Mocktail.calls
         
     | 
| 
      
 815 
     | 
    
         
            +
             
     | 
| 
      
 816 
     | 
    
         
            +
            When practicing test-driven development, you may want to ensure that a
         
     | 
| 
      
 817 
     | 
    
         
            +
            dependency wasn't called at all, and don't particularly care about the
         
     | 
| 
      
 818 
     | 
    
         
            +
            parameters. To provide a terse way to express this, Mocktail offers a top-level
         
     | 
| 
      
 819 
     | 
    
         
            +
            `calls(double, method_name = nil)` method that returns an array of the calls to
         
     | 
| 
      
 820 
     | 
    
         
            +
            the mock (optionally filtered to a particular method name) in the order they
         
     | 
| 
      
 821 
     | 
    
         
            +
            were called.
         
     | 
| 
      
 822 
     | 
    
         
            +
             
     | 
| 
      
 823 
     | 
    
         
            +
            Suppose you were writing a test of this method for example:
         
     | 
| 
      
 824 
     | 
    
         
            +
             
     | 
| 
      
 825 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 826 
     | 
    
         
            +
            def import_users
         
     | 
| 
      
 827 
     | 
    
         
            +
              users_response = @gets_users.get
         
     | 
| 
      
 828 
     | 
    
         
            +
              if users_response.success?
         
     | 
| 
      
 829 
     | 
    
         
            +
                @upserts_users.upsert(users_response.data)
         
     | 
| 
      
 830 
     | 
    
         
            +
              end
         
     | 
| 
      
 831 
     | 
    
         
            +
            end
         
     | 
| 
      
 832 
     | 
    
         
            +
            ```
         
     | 
| 
      
 833 
     | 
    
         
            +
             
     | 
| 
      
 834 
     | 
    
         
            +
            A test case of the negative branch of that `if` statement (when `success?` is
         
     | 
| 
      
 835 
     | 
    
         
            +
            false) might simply want to assert that `@upserts_users.upsert` wasn't called at
         
     | 
| 
      
 836 
     | 
    
         
            +
            all, regardless of its parameters.
         
     | 
| 
      
 837 
     | 
    
         
            +
             
     | 
| 
      
 838 
     | 
    
         
            +
            The easiest way to do this is to use `Mocktail.calls()` method, which is an
         
     | 
| 
      
 839 
     | 
    
         
            +
            alias of [Mocktail.explain(double).reference.calls](#mocktailexplain) that can
         
     | 
| 
      
 840 
     | 
    
         
            +
            filter to a specific method name. In the case of a test of the above method, you
         
     | 
| 
      
 841 
     | 
    
         
            +
            could assert:
         
     | 
| 
      
 842 
     | 
    
         
            +
             
     | 
| 
      
 843 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 844 
     | 
    
         
            +
            # Assert that the `upsert` method on the mock was never called
         
     | 
| 
      
 845 
     | 
    
         
            +
            assert_equal 0, Mocktail.calls(@upserts_users, :upsert).size
         
     | 
| 
      
 846 
     | 
    
         
            +
             
     | 
| 
      
 847 
     | 
    
         
            +
            # Assert that NO METHODS on the mock were called at all:
         
     | 
| 
      
 848 
     | 
    
         
            +
            assert_equal 0, Mocktail.calls(@upserts_users).size
         
     | 
| 
      
 849 
     | 
    
         
            +
            ```
         
     | 
| 
      
 850 
     | 
    
         
            +
             
     | 
| 
      
 851 
     | 
    
         
            +
            If you're interested in doing more complicated introspection in the nature of
         
     | 
| 
      
 852 
     | 
    
         
            +
            the calls, their ordering, and so forth, the `calls` method will return
         
     | 
| 
      
 853 
     | 
    
         
            +
            `Mocktail::Call` values with the args, kwargs, block, and information about the
         
     | 
| 
      
 854 
     | 
    
         
            +
            original class and method being mocked.
         
     | 
| 
      
 855 
     | 
    
         
            +
             
     | 
| 
      
 856 
     | 
    
         
            +
            (While this behavior can technically be accomplished with `verify(times: 0) { …
         
     | 
| 
      
 857 
     | 
    
         
            +
            }`, it's verbose and error prone to do so. Because `verify` is careful to only
         
     | 
| 
      
 858 
     | 
    
         
            +
            assert exact argument matches, it can get pretty confusing to remember to tack
         
     | 
| 
      
 859 
     | 
    
         
            +
            on `ignore_extra_args: true` and to call the method with zero args to cover all
         
     | 
| 
      
 860 
     | 
    
         
            +
            cases.)
         
     | 
| 
      
 861 
     | 
    
         
            +
             
     | 
| 
       810 
862 
     | 
    
         
             
            ### Mocktail.reset
         
     | 
| 
       811 
863 
     | 
    
         | 
| 
       812 
864 
     | 
    
         
             
            This one's simple: you probably want to call `Mocktail.reset` after each test,
         
     | 
    
        data/bin/rake
    ADDED
    
    | 
         @@ -0,0 +1,29 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #!/usr/bin/env ruby
         
     | 
| 
      
 2 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            #
         
     | 
| 
      
 5 
     | 
    
         
            +
            # This file was generated by Bundler.
         
     | 
| 
      
 6 
     | 
    
         
            +
            #
         
     | 
| 
      
 7 
     | 
    
         
            +
            # The application 'rake' is installed as part of a gem, and
         
     | 
| 
      
 8 
     | 
    
         
            +
            # this file is here to facilitate running it.
         
     | 
| 
      
 9 
     | 
    
         
            +
            #
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            require "pathname"
         
     | 
| 
      
 12 
     | 
    
         
            +
            ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
         
     | 
| 
      
 13 
     | 
    
         
            +
              Pathname.new(__FILE__).realpath)
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            bundle_binstub = File.expand_path("../bundle", __FILE__)
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            if File.file?(bundle_binstub)
         
     | 
| 
      
 18 
     | 
    
         
            +
              if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
         
     | 
| 
      
 19 
     | 
    
         
            +
                load(bundle_binstub)
         
     | 
| 
      
 20 
     | 
    
         
            +
              else
         
     | 
| 
      
 21 
     | 
    
         
            +
                abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
         
     | 
| 
      
 22 
     | 
    
         
            +
            Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
         
     | 
| 
      
 23 
     | 
    
         
            +
              end
         
     | 
| 
      
 24 
     | 
    
         
            +
            end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
            require "rubygems"
         
     | 
| 
      
 27 
     | 
    
         
            +
            require "bundler/setup"
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            load Gem.bin_path("rake", "rake")
         
     | 
| 
         @@ -0,0 +1,13 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Mocktail
         
     | 
| 
      
 2 
     | 
    
         
            +
              class CollectsCalls
         
     | 
| 
      
 3 
     | 
    
         
            +
                def collect(double, method_name)
         
     | 
| 
      
 4 
     | 
    
         
            +
                  calls = ExplainsThing.new.explain(double).reference.calls
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                  if method_name.nil?
         
     | 
| 
      
 7 
     | 
    
         
            +
                    calls
         
     | 
| 
      
 8 
     | 
    
         
            +
                  else
         
     | 
| 
      
 9 
     | 
    
         
            +
                    calls.select { |call| call.method.to_s == method_name.to_s }
         
     | 
| 
      
 10 
     | 
    
         
            +
                  end
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
              end
         
     | 
| 
      
 13 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/mocktail/version.rb
    CHANGED
    
    
    
        data/lib/mocktail.rb
    CHANGED
    
    | 
         @@ -1,3 +1,4 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require_relative "mocktail/collects_calls"
         
     | 
| 
       1 
2 
     | 
    
         
             
            require_relative "mocktail/debug"
         
     | 
| 
       2 
3 
     | 
    
         
             
            require_relative "mocktail/dsl"
         
     | 
| 
       3 
4 
     | 
    
         
             
            require_relative "mocktail/errors"
         
     | 
| 
         @@ -67,6 +68,13 @@ module Mocktail 
     | 
|
| 
       67 
68 
     | 
    
         
             
                ExplainsNils.new.explain
         
     | 
| 
       68 
69 
     | 
    
         
             
              end
         
     | 
| 
       69 
70 
     | 
    
         | 
| 
      
 71 
     | 
    
         
            +
              # An alias for Mocktail.explain(double).reference.calls
         
     | 
| 
      
 72 
     | 
    
         
            +
              # Takes an optional second parameter of the method name to filter only
         
     | 
| 
      
 73 
     | 
    
         
            +
              # calls to that method
         
     | 
| 
      
 74 
     | 
    
         
            +
              def self.calls(double, method_name = nil)
         
     | 
| 
      
 75 
     | 
    
         
            +
                CollectsCalls.new.collect(double, method_name)
         
     | 
| 
      
 76 
     | 
    
         
            +
              end
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
       70 
78 
     | 
    
         
             
              # Stores most transactional state about calls & stubbing configurations
         
     | 
| 
       71 
79 
     | 
    
         
             
              # Anything returned by this is undocumented and could change at any time, so
         
     | 
| 
       72 
80 
     | 
    
         
             
              # don't commit code that relies on it!
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: mocktail
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.2.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Justin Searls
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire:
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: exe
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2022- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2022-09-21 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies: []
         
     | 
| 
       13 
13 
     | 
    
         
             
            description:
         
     | 
| 
       14 
14 
     | 
    
         
             
            email:
         
     | 
| 
         @@ -27,8 +27,10 @@ files: 
     | 
|
| 
       27 
27 
     | 
    
         
             
            - README.md
         
     | 
| 
       28 
28 
     | 
    
         
             
            - Rakefile
         
     | 
| 
       29 
29 
     | 
    
         
             
            - bin/console
         
     | 
| 
      
 30 
     | 
    
         
            +
            - bin/rake
         
     | 
| 
       30 
31 
     | 
    
         
             
            - bin/setup
         
     | 
| 
       31 
32 
     | 
    
         
             
            - lib/mocktail.rb
         
     | 
| 
      
 33 
     | 
    
         
            +
            - lib/mocktail/collects_calls.rb
         
     | 
| 
       32 
34 
     | 
    
         
             
            - lib/mocktail/debug.rb
         
     | 
| 
       33 
35 
     | 
    
         
             
            - lib/mocktail/dsl.rb
         
     | 
| 
       34 
36 
     | 
    
         
             
            - lib/mocktail/errors.rb
         
     | 
| 
         @@ -119,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       119 
121 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       120 
122 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       121 
123 
     | 
    
         
             
            requirements: []
         
     | 
| 
       122 
     | 
    
         
            -
            rubygems_version: 3.3. 
     | 
| 
      
 124 
     | 
    
         
            +
            rubygems_version: 3.3.20
         
     | 
| 
       123 
125 
     | 
    
         
             
            signing_key:
         
     | 
| 
       124 
126 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       125 
127 
     | 
    
         
             
            summary: Take your objects, and make them a double
         
     |