/
mocha.yml
22 lines (22 loc) · 4.33 KB
/
mocha.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
---
mocha: "Mocking a class method\n product = Product.new\n Product.expects(:find).with(1).returns(product)\n assert_equal product, Product.find(1)\n\n\
Mocking an instance method on a real object\n product = Product.new\n product.expects(:save).returns(true)\n assert product.save\n\n\
Stubbing instance methods on real object\n prices = [stub(:pence => 1000), stub(:pence => 2000)]\n product = Product.new\n product.stubs(:prices).returns(prices)\n assert_equal [1000, 2000], product.prices.collect {|p| p.pence}\n\n\
Stubbing an instance method on all instances of a class\n Product.any_instance.stubs(:name).returns('stubbed_name')\n product = Product.new\n assert_equal 'stubbed_name', product.name\n\n\
Traditional mocking\n object = mock()\n object.expects(:expected_method).with(:p1, :p2).returns(:result)\n assert_equal :result, object.expected_method(:p1, :p2)\n\n\
Shortcuts\n object = stub(:method1 => :result1, :method2 => :result2)\n assert_equal :result1, object.method1\n assert_equal :result2, object.method2\n\n\
Expection Methods:\n at_least(minimum) \n Modifies expectation so that the expected method must be called at least a minimum number of times.\n\n at_least_once\n Modifies expectation so that the expected method must be called at least once.\n\n never\n Modifies expectation so that the expected method must never be called.\n\n raises(exception = RuntimeError, message = nil)\n Modifies expectation so that when the expected method is called, it raises the specified exception with the specified message.\n\n returns(value)\n Modifies expectation so that when the expected method is called, it returns the specified value.\n\n times(range)\n Modifies expectation so that the number of calls to the expected method must be within a specific range.\n\n with(*arguments, &block)\n Modifies expectation so that the expected method must be called with specified arguments.\n\n yields(*parameters)\n Modifies expectation so that when the expected method is called, it yields with the specified parameters.\n \n \n\
Parameter Matches:\n\
==================\n\n\
== Not(parameter_matcher) \xE2\x86\x92 parameter_matcher\n\n\
Matches if parameter_matcher does not match.\n\n object = mock()\n object.expects(:method_1).with(Not(includes(1)))\n object.method_1([0, 2, 3])\n # no error raised\n\n object = mock()\n object.expects(:method_1).with(Not(includes(1)))\n object.method_1([0, 1, 2, 3])\n # error raised, because method_1 was not called with object not including 1\n\n\
== all_of(*parameter_matchers) \xE2\x86\x92 parameter_matcher\n\n\
Matches if all parameter_matchers match.\n\n object = mock()\n object.expects(:method_1).with(all_of(includes(1), includes(3)))\n object.method_1([1, 3])\n # no error raised\n\n object = mock()\n object.expects(:method_1).with(all_of(includes(1), includes(3)))\n object.method_1([1, 2])\n # error raised, because method_1 was not called with object including 1 and 3\n\n\
== any_of(*parameter_matchers) \xE2\x86\x92 parameter_matcher\n\n\
Matches if any parameter_matchers match.\n\n object = mock()\n object.expects(:method_1).with(any_of(1, 3))\n object.method_1(1)\n # no error raised\n\n object = mock()\n object.expects(:method_1).with(any_of(1, 3))\n object.method_1(3)\n # no error raised\n\n object = mock()\n object.expects(:method_1).with(any_of(1, 3))\n object.method_1(2)\n # error raised, because method_1 was not called with 1 or 3\n\n\n\
== any_parameters() \xE2\x86\x92 parameter_matcher\n\n\
Matches any parameters.\n\n object = mock()\n object.expects(:method_1).with(any_parameters)\n object.method_1(1, 2, 3, 4)\n # no error raised\n\n object = mock()\n object.expects(:method_1).with(any_parameters)\n object.method_1(5, 6, 7, 8, 9, 0)\n # no error raised\n\n\n\
== anything() \xE2\x86\x92 parameter_matcher\n\n\
Matches any object.\n\n object = mock()\n object.expects(:method_1).with(anything)\n object.method_1('foo')\n # no error raised\n\n\
== equals(value) \xE2\x86\x92 parameter_matcher\n\n\
Matches Object equalling value.\n\n object = mock()\n object.expects(:method_1).with(equals(2))\n object.method_1(2)\n # no error raised\n\n object = mock()\n object.expects(:method_1).with(equals(2))\n object.method_1(3)\n # error raised, because method_1 was not called with Object equalling 3"