Giter Club home page Giter Club logo

minitest-mock_expectations's Introduction

minitest mock expectations

Provides method call assertions for minitest.

Installation

Add this line to your application's Gemfile:

gem "minitest-mock_expectations"

And then execute:

$ bundle

Or install it as:

$ gem install minitest-mock_expectations

Usage

require "minitest/mock_expectations"

Imagine we have model Post:

class Post
  attr_accessor :title, :body
  attr_reader :comments

  def initialize(title: "", body: "", comments: [])
    @title = title
    @body = body
    @comments = comments
  end

  def add_comment(comment)
    @comments << comment

    "Thank you!"
  end
end

and @post variable that refers to an instance of Post:

def setup
  @post = Post.new(
    title: "What is new in Rails 6.0",
    body: "https://gitlab.com/bogdanvlviv/posts/-/issues/15",
    comments: [
      "Wow.",
      "I like this post."
    ]
  )
end

assert_called(object, method_name, message = nil, times: 1, returns: nil)

Asserts that the method will be called on the object in the block

assert_called(@post, :title) do
  @post.title
end

To assert that the method will be called multiple times on the object in the block set :times option:

assert_called(@post, :title, times: 2) do
  @post.title
  @post.title
end

You can stub the return value of the method in the block via :returns option:

assert_called(@post, :title, returns: "What is new in Rails 5.2") do
  assert_equal "What is new in Rails 5.2", @object.title
end

assert_equal "What is new in Rails 6.0", @object.title

refute_called(object, method_name, message = nil, &block)

Asserts that the method will not be called on the object in the block

refute_called(@post, :title) do
  @post.body
end

assert_not_called

Alias for refute_called.

assert_called_with(object, method_name, arguments, returns: nil)

Asserts that the method will be called with the arguments on the object in the block

assert_called_with(@post, :add_comment, ["Thanks for sharing this."]) do
  @post.add_comment("Thanks for sharing this.")
end

You can stub the return value of the method in the block via :returns option:

assert_called_with(@post, :add_comment, ["Thanks for sharing this."], returns: "Thanks!") do
  assert_equal "Thanks!", @post.add_comment("Thanks for sharing this.")
end

assert_equal "Thank you!", @post.add_comment("Thanks for sharing this.")

You can also assert that the method will be called with different arguments on the object in the block:

assert_called_with(@post, :add_comment, [["Thanks for sharing this."], ["Thanks!"]]) do
  @post.add_comment("Thanks for sharing this.")
  @post.add_comment("Thanks!")
end
assert_called_with(@post, :add_comment, [[["Thanks for sharing this."]]]) do
  @post.add_comment(["Thanks for sharing this."])
end
assert_called_with(@post, :add_comment, [[["Thanks for sharing this.", "Thanks!"]]]) do
  @post.add_comment(["Thanks for sharing this.", "Thanks!"])
end
assert_called_with(@post, :add_comment, [[["Thanks for sharing this."], ["Thanks!"]]]) do
  @post.add_comment(["Thanks for sharing this."], ["Thanks!"])
end
assert_called_with(@post, :add_comment, [["Thanks for sharing this."], {body: "Thanks!"}]) do
  @post.add_comment(["Thanks for sharing this."], {body: "Thanks!"})
end
assert_called_with(@post, :add_comment, [[["Thanks for sharing this."]], [{body: "Thanks!"}]]) do
  @post.add_comment(["Thanks for sharing this."])
  @post.add_comment({body: "Thanks!"})
end
assert_called_with(@post, :add_comment, [[["Thanks for sharing this."]], [["Thanks!"]]]) do
  @post.add_comment(["Thanks for sharing this."])
  @post.add_comment(["Thanks!"])
end

assert_called_on_instance_of(klass, method_name, message = nil, times: 1, returns: nil)

Asserts that the method will be called on an instance of the klass in the block

assert_called_on_instance_of(Post, :title) do
  @post.title
end

To assert that the method will be called multiple times on an instance of the klass in the block set :times option:

assert_called_on_instance_of(Post, :title, times: 2) do
  @post.title
  @post.title
end

You can stub the return value of the method in the block via :returns option:

assert_called_on_instance_of(Post, :title, returns: "What is new in Rails 5.2") do
  assert_equal "What is new in Rails 5.2", @post.title
end

assert_equal "What is new in Rails 6.0", @post.title

Use nesting of the blocks to assert that the several methods will be called on an instance of the klass in the block:

assert_called_on_instance_of(Post, :title, times: 3) do
  assert_called_on_instance_of(Post, :body, times: 2) do
    @post.title
    @post.body
    @post.title
    @post.body
    @post.title
  end
end

refute_called_on_instance_of(klass, method_name, message = nil, &block)

Asserts that the method will not be called on an instance of the klass in the block

refute_called_on_instance_of(Post, :title) do
  @post.body
end

Use nesting of the blocks to assert that the several methods will not be called on an instance of the klass in the block:

refute_called_on_instance_of(Post, :title) do
  refute_called_on_instance_of(Post, :body) do
    @post.add_comment("Thanks for sharing this.")
  end
end

assert_not_called_on_instance_of

Alias for refute_called_on_instance_of.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bogdanvlviv/minitest-mock_expectations. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

minitest-mock_expectations's People

Contributors

bogdanvlviv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

thatguysimon

minitest-mock_expectations's Issues

Thanks and minor suggestion

Hey, this is a great life saving gem. It brings the nice RSpec expectations easily and intuitively into my Rails projects, so thanks for that.

One thing I noticed - it is hard to find this gem. Searching "minitest mock expectations" does not show this repo anywhere in the first pages, and I don't remember even how I did find it - perhaps changing the README H1 to "Minitest Mock Expectations" will help Google?

Asserting call to method with array argument fails

Hi, great job on this gem!
I found a pretty serious bug though :(

If you try to assert a call to a method that takes an array as its argument:

assert_called_with(Tempfile, :new, [['hello', '.jpg']]) do
  file = Tempfile.new(['hello', '.jpg'])  
end

Running the above code will result in:

ArgumentError: mocked method :call expects 2 arguments, got 1
from .../ruby-2.6.3/gems/minitest-5.14.0/lib/minitest/mock.rb:150:in `method_missing'

This happens due to a collision with the feature of being able to pass an array of different sets of arguments that the method can be called with.

I suggest changing the interface of the multiple arguments feature to the following (which will also allow providing a different return value for each set of arguments):

expectations = [
  { 
    expected_args: ["Thanks for sharing this."],
    return_value: mocked_comment_1
  },
  { 
    expected_args:  ["Thanks!"],
    return_value: mocked_comment_2 
  }
]

assert_called_with(@post, :add_comment, expectations: expectations) do
  @post.add_comment("Thanks for sharing this.")
  @post.add_comment("Thanks!")
end

Shall I open a PR for this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.