-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create Mock calls constructor #16
Comments
A workaround is to overload the constructor. Declare function used for mock creation:
Create mock: |
@travelerspb I can confirm the constructor is called intentionally during mock creation. Having a fully initialized object makes it possible to call the base class implementation when there's no overriding setup (this behavior may be enabled by setting Still I believe you've made a valid point. @matthieubouvierthales Thanks for providing a workaround. Let me know what you think. |
I agree that the original class's constructor should not be called. We are attempting to mock Angular 2's Router class, and aren't able to because the constructor for Router is complex and has several expectations of how the various fields behave that don't matter for our test. We only care about the external interface of the object, not its internals. |
@nertzy Have you tried to construct a Router mock by providing mocks as constructor arguments? Related to what would happen if the constructor is not called by Mock.ofType, based on the output from TypeScript, it looks like if the constructor is not called then the instance properties are not getting defined and so they're not mockable anymore.
TS output:
|
Closing this. IMO losing access to mock instance properties is show stop. To avoid running constructor logic irrelevant for mock classes, you could provide mocks as constructor arguments. class Type1 {
...
}
class Type2 {
...
}
class Foo {
constructor(p1: Type1, p2: Type2) {
...
}
}
let mockA1: TypeMoq.Mock<Type1> = TypeMoq.Mock.ofType(Type1);
let mockA2: TypeMoq.Mock<Type2> = TypeMoq.Mock.ofType(Type2);
let mockFoo: TypeMoq.Mock<Foo> = TypeMoq.Mock.ofType2(Foo, [mockA1.object, mockA2.object]); |
What if one of the constructor arguments also has a constructor that requires arguments? It's unfortunate if there are technical limitations preventing a proper solution. In its current state however, I find TypeMoq to be unusable for me. |
If a constructor argument has a constructor that requires arguments, you can mock the args as necessary. class Type1 {
...
}
class Type2 {
...
}
class Type3 {
...
}
class Type4 {
constructor(p1: Type1, p2: Type2) {
...
}
}
class Foo {
constructor(p1: Type3, p2: Type4) {
...
}
}
let mockA1: TypeMoq.Mock<Type1> = TypeMoq.Mock.ofType(Type1);
let mockA2: TypeMoq.Mock<Type2> = TypeMoq.Mock.ofType(Type2);
let mockA3: TypeMoq.Mock<Type3> = TypeMoq.Mock.ofType(Type3);
let mockA4: TypeMoq.Mock<Type4> = TypeMoq.Mock.ofType2(Type4, [mockA1.object, mockA2.object]);
let mockFoo: TypeMoq.Mock<Foo> = TypeMoq.Mock.ofType2(Foo, [mockA3.object, mockA4.object]); |
But if another and another mock also has constructor arguments I can get crazy setup for simple test. |
You can omit any constructor argument if For the example above, creating let mockFoo: TypeMoq.Mock<Foo> = TypeMoq.Mock.ofType(Foo); or if just one of the constructor params needs to be mocked: let mockA3: TypeMoq.Mock<Type3> = TypeMoq.Mock.ofType(Type3);
let mockFoo: TypeMoq.Mock<Foo> = TypeMoq.Mock.ofType2(Foo, [mockA3.object]); |
IMO, no matter how complex, I should be able to mock
becomes:
I'd like to avoid:
I wouldn't mind opting out of constructor consideration: |
Starting with version 1.0.0, typemoq supports dynamic mocking that does exactly what you want. Simply create the mock like this and the constructor of your class is not going to get called:
|
Im trying to setup mock object like thus
let account: TypeMoq.Mock<IAccount> = TypeMoq.Mock.ofType<IAccount>(Account);
Account class has a constructor, and this constructor is called during Mock object creation.
Im not sure if this is made intentionally or not, but obviously when we write test we want to avoid any logic in mocked classes
The text was updated successfully, but these errors were encountered: