|
1 |
| -import {anything, instance, mock, reset, verify, when} from "../src/ts-mockito"; |
| 1 | +import {anything, instance, mock, reset, resetStubs, verify, when} from "../src/ts-mockito"; |
2 | 2 | import {Foo} from "./utils/Foo";
|
3 | 3 |
|
4 | 4 | describe("resetting mocked object", () => {
|
@@ -163,3 +163,101 @@ describe("resetting mocked object", () => {
|
163 | 163 | });
|
164 | 164 | });
|
165 | 165 | });
|
| 166 | + |
| 167 | +describe("resetting mocked object stubs", () => { |
| 168 | + let mockedFoo: Foo; |
| 169 | + let foo: Foo; |
| 170 | + |
| 171 | + beforeEach(() => { |
| 172 | + mockedFoo = mock(Foo); |
| 173 | + foo = instance(mockedFoo); |
| 174 | + }); |
| 175 | + |
| 176 | + describe("when method has been called once", () => { |
| 177 | + describe("but later stub has been reset", () => { |
| 178 | + it("shows that still been called", () => { |
| 179 | + // given |
| 180 | + foo.getBar(); |
| 181 | + verify(mockedFoo.getBar()).once(); |
| 182 | + |
| 183 | + // when |
| 184 | + resetStubs(mockedFoo); |
| 185 | + |
| 186 | + // then |
| 187 | + verify(mockedFoo.getBar()).once(); |
| 188 | + }); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + describe("when two different methods has been called twice", () => { |
| 193 | + describe("but later stub has been reset", () => { |
| 194 | + it("shows that still been called", () => { |
| 195 | + // given |
| 196 | + foo.getBar(); |
| 197 | + foo.getBar(); |
| 198 | + foo.sumTwoNumbers(2, 3); |
| 199 | + foo.sumTwoNumbers(2, 3); |
| 200 | + verify(mockedFoo.getBar()).twice(); |
| 201 | + verify(mockedFoo.sumTwoNumbers(2, 3)).twice(); |
| 202 | + |
| 203 | + // when |
| 204 | + resetStubs(mockedFoo); |
| 205 | + |
| 206 | + // then |
| 207 | + verify(mockedFoo.getBar()).twice(); |
| 208 | + verify(mockedFoo.sumTwoNumbers(2, 3)).twice(); |
| 209 | + verify(mockedFoo.sumTwoNumbers(anything(), anything())).twice(); |
| 210 | + }); |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
| 214 | + describe("when two different methods has been called", () => { |
| 215 | + describe("but later stub has been reset", () => { |
| 216 | + it("throws exception with information that methods has still been called", () => { |
| 217 | + // given |
| 218 | + foo.getBar(); |
| 219 | + foo.sumTwoNumbers(2, 3); |
| 220 | + verify(mockedFoo.getBar()).calledBefore(mockedFoo.sumTwoNumbers(2, 3)); |
| 221 | + |
| 222 | + // when |
| 223 | + resetStubs(mockedFoo); |
| 224 | + |
| 225 | + // then |
| 226 | + verify(mockedFoo.getBar()).calledBefore(mockedFoo.sumTwoNumbers(2, 3)); |
| 227 | + }); |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
| 231 | + describe("when object has been stubbed", () => { |
| 232 | + describe("but later stub has been reset", () => { |
| 233 | + it("should reset configured stubs", () => { |
| 234 | + // given |
| 235 | + when(mockedFoo.convertNumberToString(3)).thenReturn("three"); |
| 236 | + resetStubs(mockedFoo); |
| 237 | + |
| 238 | + // when |
| 239 | + const result: string = foo.convertNumberToString(3); |
| 240 | + |
| 241 | + // then |
| 242 | + expect(result).toBeNull(); |
| 243 | + }); |
| 244 | + }); |
| 245 | + }); |
| 246 | + |
| 247 | + describe("when object has been stubbed", () => { |
| 248 | + describe("but later stub has been reset", () => { |
| 249 | + it("should be able to setup new stubs", () => { |
| 250 | + // given |
| 251 | + when(mockedFoo.convertNumberToString(3)).thenReturn("three"); |
| 252 | + resetStubs(mockedFoo); |
| 253 | + when(mockedFoo.convertNumberToString(3)).thenReturn("newStubbedValue"); |
| 254 | + |
| 255 | + // when |
| 256 | + const result: string = foo.convertNumberToString(3); |
| 257 | + |
| 258 | + // then |
| 259 | + expect(result).toEqual("newStubbedValue"); |
| 260 | + }); |
| 261 | + }); |
| 262 | + }); |
| 263 | +}); |
0 commit comments