1 module mocked.tests.stub;
2 
3 import dshould;
4 import mocked;
5 
6 @("class can be stubbed")
7 unittest
8 {
9     static class Dependency
10     {
11         bool isEven(int number)
12         {
13             return (number & 1) == 0;
14         }
15     }
16     Mocker mocker;
17     auto stubbed = mocker.stub!Dependency;
18 
19     stubbed.stub.isEven(6).returns(false);
20     stubbed.stub.isEven(5).returns(true);
21 
22     Dependency dependency = stubbed.get;
23 
24     dependency.isEven(5).should.equal(true);
25     dependency.isEven(6).should.equal(false);
26 }
27 
28 @("stubs classes with an constructor")
29 unittest
30 {
31     static class Dependency
32     {
33         string phrase;
34 
35         public this(string phrase)
36         {
37             this.phrase = phrase;
38         }
39 
40         public string saySomething()
41         {
42             return this.phrase;
43         }
44     }
45     auto dependency = Mocker().stub!Dependency("Alea iacta est.");
46 
47     dependency.stub.saySomething().passThrough;
48 
49     dependency.saySomething().should.equal("Alea iacta est.");
50 }
51 
52 @("can use custom comparator")
53 unittest
54 {
55     import std.math : fabs;
56 
57     static class Dependency
58     {
59         public bool call(float)
60         {
61             return false;
62         }
63     }
64 
65     alias approxComparator = (float a, float b) {
66         return fabs(a - b) <= 0.1;
67     };
68     auto mocker = configure!(Comparator!approxComparator);
69     auto builder = mocker.stub!Dependency;
70 
71     builder.stub.call(1.01).returns(true);
72 
73     auto stub = builder.get;
74 
75     stub.call(1.02).should.be(true);
76 }
77 
78 @("stubs const methods")
79 unittest
80 {
81     interface I
82     {
83         public bool isI(string) const;
84     }
85     Mocker mocker;
86 
87     static assert(is(typeof(mocker.stub!I())));
88 }