-
Notifications
You must be signed in to change notification settings - Fork 21
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
InvalidCastException : Unable to cast object of type 'T' to type 'Moq.IMocked`1[T]' #9
Comments
I'm not going to lie, this integration library was somewhat of a "drive-by contribution" where none of the actual Autofac authors are "Moq experts" so it may take a bit for us to get to figuring out what's up here. I think either @kzu or @tkellogg wrote this. Looking at our unit tests with abstract classes they use If that doesn't fix you up... honestly, there's not much code here - a couple of classes. It may get you an answer faster if you copy the two classes into your project and debug into it to see if there's a real bug or if there's something wrong with your expectations. I don't mean that to sound like we're punting on you, but there's a lot slipping through the cracks right now and I don't want you to get blocked up waiting on an answer that may be a long time coming. |
I'm 99% certain there's a real bug here. Actually, this exception will occur any time the Here's why it happens: Firstly, Secondly, the When a concrete classed is used as the type for I think the I will be working on this in my free time, which I have a little of today. |
This is definitely a bug, because Moq does support creating mocks of concrete classes! |
I have committed a fix for this issue and pushed a https://www.nuget.org/packages/Autofac.Extras.Moq I took the Gist from @dlurton and turned it into a unit test fixture which is now passing. The .NET Framework target has been dropped to Moq has been updated to Please take it for a spin and let me know how it goes. |
Thanks for doing this. But unfortunately it has broken something else now: Calling Only calling
It seems that calling |
Thanks for the quick feedback @shaynevanasperen. Sounds like some more tests are needed. I'll try to get back on to it tomorrow. |
I have pushed Both the I added a few more unit tests targeting the new class case. Fingers crossed this does the trick. 🤞 |
Thank you @alexmg! This works now. |
Using 4.0.0 of Autofac.Extras.Moq, with these classes:
The only one I can use
Autofac.Extras.Mock.AutoMock.Mock<T>()
to create a mock of isSomeAbstractClass
. All others fail withInvalidCastException
inside ofAutofac.Extras.Moq.AutoMock.Mock<T>()
. For example:However, using regular old
Moq.Mock<T>
works just fine. Here's a gist with some test cases demonstrating these exceptions and that Moq.Mock works.Either I'm missing something or there be a bug here...
The text was updated successfully, but these errors were encountered: