Skip to content

Commit ec4aa51

Browse files
committed
fix for scenario where proxies were interfering with names of objects.
1 parent f69ea69 commit ec4aa51

21 files changed

+411
-159
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,27 @@ console.log(fakeCalculator.divide(9, 5)); //prints 1338
130130
- Doesn't weigh much.
131131
- Produces very clean and descriptive error messages. Try it out - you'll love it.
132132
- Doesn't rely on object instances - you can produce a strong-typed fake from nothing, ensuring that everything is mocked.
133+
134+
# Beware
135+
Let's say we have a class with a method called `received`, `didNotReceive` or `mimick` keyword - how do we mock it?
136+
137+
Simple! We disable the proxy methods temporarily while invoking the method by using the `disableFor` method which disables these special methods.
138+
139+
```typescript
140+
class Example {
141+
received(someNumber: number) {
142+
console.log(someNumber);
143+
}
144+
}
145+
146+
var fake = Substitute.for<Example>();
147+
148+
//BAD: this would have called substitute.js' "received" method.
149+
//fake.received(2);
150+
151+
//GOOD: we now call the "received" method we have defined in the class above.
152+
Substitute.disableFor(fake).received(1337);
153+
154+
//now we can assert that we received a call to the "received" method.
155+
fake.received().received(1337);
156+
```

dist/spec/index.test.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export declare class Example {
33
c(arg1: string, arg2: string): string;
44
readonly d: number;
55
v: string;
6+
received(stuff: number | string): void;
67
foo(): string;
78
}

dist/spec/index.test.js

Lines changed: 35 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/spec/index.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/Context.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export declare class ProxyObjectContext {
4141
findActualPropertyCalls(propertyName: string): ProxyCallRecord[];
4242
findActualMethodCalls(propertyName: string, args?: any[]): ProxyCallRecord[];
4343
getLastCall(): ProxyCallRecord;
44-
addActualPropertyCall(): void;
44+
removeActualPropertyCall(call: ProxyCallRecord): void;
45+
addActualPropertyCall(): ProxyCallRecord;
4546
fixExistingCallArguments(): void;
4647
}
4748
export declare class ProxyCallRecord {

dist/src/Context.js

Lines changed: 50 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/Context.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/Index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Substitute } from './Substitute';
2-
export { ObjectSubstitute, PropertySubstitute, FunctionSubstitute } from './Transformations';
32
export { Arg } from './Arguments';
43
export { Substitute };
54
export default Substitute;

dist/src/Index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/Substitute.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { ObjectSubstitute } from "./Transformations";
1+
import { ObjectSubstitute, OmitProxyMethods, DisabledSubstituteObject } from "./Transformations";
22
export declare class Substitute {
3-
static for<T>(): ObjectSubstitute<T>;
3+
static disableFor<T extends ObjectSubstitute<OmitProxyMethods<any>>>(substitute: T): DisabledSubstituteObject<T>;
4+
static for<T>(): ObjectSubstitute<OmitProxyMethods<T>, T>;
45
private static assertCallMatchCount;
56
}

0 commit comments

Comments
 (0)