Popularity
4.8
Declining
Activity
0.0
Stable
13
3
1

Programming language: Crystal
Tags: Testing    

mock alternatives and similar shards

Based on the "Testing" category

Do you think we are missing an alternative of mock or a related project?

Add another 'Testing' Shard

README

Mock Build Status

Mock is a doubles (stubs and mocks) library for Crystal, inspired by the API of rspec-mocks (but it implements a small basic subset of it to the date).

Installation

Add this to your application's shard.yml:

development_dependencies:
  mock:
    github: porras/mock

You can now run shards to install it.

Usage

Require it in your tests and you can start using it.

require "mock"

Creating a double

Just call the double() method.

Stubbing a method

Calling stub on that double object will set a method stub:

my_object = double()
my_object.stub(:my_method)

You can establish a return value for the stub method (if you don't, method stubs return nil):

my_object = double()
my_object.stub(:my_method).and_return("my value")

my_object.my_method.should eq("my value")

You can also filter my arguments, establishing different stubs for the same method:

my_object = double()
my_object.stub(:my_method).with(1).and_return("value 1")
my_object.stub(:my_method).with(2).and_return("value 2")

my_object.my_method(1).should eq("value 1")
my_object.my_method(2).should eq("value 2")

Setting expectations

You can also set the expectation that a method will be called, and it will be automatically checked at the end of the test:

my_object = double()
my_object.should_receive(:my_method).with(1).and_return("my value")

# if we omit this line, the test will fail
my_object.my_method(1).should eq("value 1")

See example_spec.cr for more examples.

Contributing

  1. Fork it ( https://github.com/porras/mock/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

This code is released under the MIT License.

Contributors


*Note that all licence references and agreements mentioned in the mock README section above are relevant to that project's source code only.