-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement mocking for properties without getters by using es6 Proxy
- Loading branch information
1 parent
9e9a187
commit bc03ca6
Showing
6 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"declaration": true, | ||
"lib": [ | ||
"es5", | ||
"es6", | ||
"dom" | ||
], | ||
"types": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {MethodToStub} from "../src/MethodToStub"; | ||
import {instance, mock, verify, when} from "../src/ts-mockito"; | ||
import {Bar} from "./utils/Bar"; | ||
|
||
describe("mocking", () => { | ||
let mockedFoo: FooWithProperties; | ||
let foo: FooWithProperties; | ||
|
||
describe("mocking object with properties (that don't have getters)", () => { | ||
it("does create own property descriptors on mock after when is called", () => { | ||
// given | ||
|
||
// when | ||
mockedFoo = mock(FooWithProperties); | ||
when(mockedFoo.sampleNumber).thenReturn(42); | ||
|
||
// then | ||
expect((mockedFoo.sampleNumber as any) instanceof MethodToStub).toBe(true); | ||
}); | ||
|
||
it("does create own property descriptors on instance", () => { | ||
// given | ||
mockedFoo = mock(FooWithProperties); | ||
foo = instance(mockedFoo); | ||
|
||
// when | ||
when(mockedFoo.sampleNumber).thenReturn(42); | ||
|
||
// then | ||
expect(foo.sampleNumber).toBe(42); | ||
}); | ||
|
||
it("works with verification if property is stubbed", () => { | ||
// given | ||
mockedFoo = mock(FooWithProperties); | ||
foo = instance(mockedFoo); | ||
when(mockedFoo.sampleNumber).thenReturn(42); | ||
|
||
// when | ||
const value = foo.sampleNumber; | ||
|
||
// then | ||
expect(() => verify(mockedFoo.sampleNumber).once()).not.toThrow(); | ||
}); | ||
|
||
it("works with verification if property is unstubbed", () => { | ||
// given | ||
mockedFoo = mock(FooWithProperties); | ||
foo = instance(mockedFoo); | ||
|
||
// when | ||
const value = foo.sampleNumber; | ||
|
||
// then | ||
expect(() => verify(mockedFoo.sampleNumber).once()).not.toThrow(); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
class FooWithProperties { | ||
public readonly sampleNumber: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"declaration": true, | ||
"lib": [ | ||
"es5", | ||
"es6", | ||
"dom" | ||
], | ||
"types": [ | ||
|