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).
Add this to your application's shard.yml:
development_dependencies: mock: github: porras/mock
You can now run
shards to install it.
Require it in your tests and you can start using it.
Creating a double
Just call the
Stubbing a method
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
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")
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.
- Fork it ( https://github.com/porras/mock/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
This code is released under the MIT License.