1 module mocked.tests.expectations; 2 3 import dshould; 4 import dshould.ShouldType; 5 import mocked; 6 import std.algorithm; 7 import unit_threaded : ShouldFail; 8 9 void startWith(Should, T)(Should should, T expected, 10 Fence _ = Fence(), string file = __FILE__, size_t line = __LINE__) 11 if (isInstanceOf!(ShouldType, Should)) 12 { 13 auto got = should.got; 14 should.check(got.startsWith(expected), expected, got, file, line); 15 } 16 17 @ShouldFail("if an expected method hasn't been called") 18 unittest 19 { 20 static class Dependency 21 { 22 string say(string phrase) 23 { 24 return phrase; 25 } 26 } 27 with (Mocker()) 28 { 29 auto dependency = mock!Dependency; 30 31 dependency.expect.say("Man geht zu Grunde,") 32 .returns("wenn man immer zu den Gründen geht."); 33 } 34 } 35 36 @("repeats expectations infinitely") 37 unittest 38 { 39 enum expected = "Piscis primum a capite foetat"; 40 41 Mocker mocker; 42 43 auto mock = mocker.mock!Object; 44 45 mock.expect.toString().returns(expected).repeatAny; 46 47 Object object = mock.get; 48 49 object.toString.should.equal(expected); 50 object.toString.should.equal(expected); 51 52 mocker.verify; 53 } 54 55 @("prints arguments in the unexpected call error message") 56 unittest 57 { 58 static class Dependency 59 { 60 void say(string phrase1, string phrase2) 61 { 62 } 63 } 64 Mocker mocker; 65 66 auto mock = mocker.mock!Dependency.get; 67 68 mock.say("Ton der Jugend", "zu laut.") 69 .should.throwAn!UnexpectedCallError 70 .where.toString 71 .should.startWith(`Unexpected call: Dependency.say("Ton der Jugend", "zu laut.")`); 72 } 73 74 @("throws once") 75 unittest 76 { 77 static class Dependency 78 { 79 void say(string phrase) 80 { 81 } 82 } 83 Mocker mocker; 84 85 auto mock = mocker.mock!Dependency; 86 87 mock.expect.say("Die Ängstlichkeit vergiftet die Seele.").repeat(2); 88 89 mocker.verify.should.throwAn!ExpectationViolationException; 90 mocker.verify.should.not.throwAn!ExpectationViolationException; 91 } 92 93 @("gives multiline error messages on mismatched arguments") 94 unittest 95 { 96 static class Dependency 97 { 98 void say(string phrase) 99 { 100 } 101 } 102 Mocker mocker; 103 104 auto mock = mocker.mock!Dependency; 105 106 mock.expect.say("Let's eat, grandma!").repeat(2); 107 108 mock.say("Let's eat grandma!") 109 .should.throwAn!UnexpectedArgumentError 110 .where.toString.should.contain.any("\n"); 111 } 112 113 @("verify prints what method was expected") 114 unittest 115 { 116 enum string phrase = "Täglich erstaune ich: ich kenne mich selber nicht!"; 117 static class Dependency 118 { 119 void say(string) 120 { 121 } 122 } 123 Mocker mocker; 124 125 auto mock = mocker.mock!Dependency; 126 127 mock.expect.say(phrase); 128 129 mocker.verify 130 .should.throwAn!ExpectationViolationException 131 .where.toString.should.startWith( 132 `Expected method not called: Dependency.say("` ~ phrase ~ `")` 133 ); 134 } 135 136 @("allows to pick the overload without specifying the arguments") 137 unittest 138 { 139 enum string expected = "ad nullius rei essentiam pertinet existentia"; 140 static class Dependency 141 { 142 string show(bool x) 143 { 144 return x ? "true" : "false"; 145 } 146 147 string show(string x) 148 { 149 return x; 150 } 151 } 152 Mocker mocker; 153 154 auto mock = mocker.mock!Dependency; 155 156 mock.expect.show!string.returns(expected); 157 158 auto dependency = mock.get; 159 160 dependency.show(null).should.equal(expected); 161 } 162 163 @("allows to pick the overload without specifying the arguments") 164 unittest 165 { 166 static class Dependency 167 { 168 void show(string, string) 169 { 170 } 171 } 172 Mocker mocker; 173 auto mock = mocker.mock!Dependency; 174 175 static assert(!is(typeof(mock.expect.show!string))); 176 }