伪造/嘲讽的界面给人"没有默认构造函数"错误,怎么可能呢? [英] Faking/mocking an interface gives "no default constructor" error, how can that be?
问题描述
我想写一个仓库实现的单元测试。知识库使用RavenDB作为数据库。对于单元测试,我想嘲笑RavenDB部分。为了创建嘲笑(假货)我使用FakeItEasy。我想就不会有用嘲讽/伪造,因为RavenDB API通过接口访问的任何问题。
I'm trying to write a unit test of a repository implementation. The repository uses RavenDB as a database. For the unit tests, I would like to mock the RavenDB parts. In order to create the mocks (fakes) I'm using FakeItEasy. I figured there wouldn't be any problems with the mocking/faking since the RavenDB API is accessed through interfaces.
我做的不过试图实例化一个特定的时候有一个问题嘲笑。我的单元测试代码的相关部分看起来是这样的:
I do however have a problem when trying to instantiate a specific mock. The relevant parts of my unit test code looks like this:
[Fact]
public void Test() {
UserDocument doc = ...;
IQueryable<UserDocument> where = A.Fake<IQueryable<UserDocument>>();
A.CallTo(() => where.First()).Returns(doc);
IRavenQueryable<UserDocument> query = A.Fake<IRavenQueryable<UserDocument>>();
IDocumentSession session = A.Fake<IDocumentSession>();
A.CallTo(() => session.Query<UserDocument>()).Returns(query);
IDocumentStore store = A.Fake<IDocumentStore>();
A.CallTo(() => store.OpenSession()).Returns(session);
.
.
.
}
在实例化IRavenQueryable假我得到一个异常。这是从Xunit.net亚军日志:
When instantiating the IRavenQueryable fake I get an exception. This is the log from the Xunit.net runner:
UnitTest.Test : FakeItEasy.Core.FakeCreationException :
Failed to create fake of type "System.Linq.IQueryable`1[UserDocument]".
Below is a list of reasons for failure per attempted constructor:
No constructor arguments failed:
No default constructor was found on the type System.Linq.IQueryable`1[UserDocument].
Stack Trace:
vid FakeItEasy.Core.DefaultExceptionThrower.ThrowFailedToGenerateProxyWithResolvedConstructors(Type typeOfFake, String reasonForFailureOfUnspecifiedConstructor, IEnumerable`1 resolvedConstructors)
vid FakeItEasy.Creation.FakeObjectCreator.TryCreateFakeWithDummyArgumentsForConstructor(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, String failReasonForDefaultConstructor, Boolean throwOnFailure)
vid FakeItEasy.Creation.FakeObjectCreator.CreateFake(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, Boolean throwOnFailure)
vid FakeItEasy.Creation.DefaultFakeAndDummyManager.CreateFake(Type typeOfFake, FakeOptions options)
vid FakeItEasy.Creation.DefaultFakeCreatorFacade.CreateFake[T](Action`1 options)
的
没有发现默认的构造函数没有任何意义,因为什么,我试图伪装是一个接口。有没有人有一个建议,这个问题可能是什么?
The "no default constructor found" doesn't make any sense since what I'm trying to fake is an interface. Does anyone have a suggestion what the problem might be?
推荐答案
您是在异常消息没有任何意义正确,这是一个错误。这将是巨大的,如果你能提供一个VS的解决方案,再现了错误,并在这里提交一个问题:的https: //github.com/patrik-hagne/FakeItEasy/
You're correct in that the exception message does not make any sense, this is a bug. It would be great if you could supply a VS-solution that reproduces the bug and file an issue here: https://github.com/patrik-hagne/FakeItEasy/
该错误是在错误的异常信息被使用,但是一定有什么不对的使得假冒创造出问题。是UserDocument中型上市?如果是内部和你给通过使用InternalsVisibleToAttribute的测试项目获得它必须给代理生成库访问它,以及:的 https://github.com/FakeItEasy/FakeItEasy/wiki/How-to-fake-internal-(朋友-in-VB)-types 。
The bug is in that the wrong exception message is used, however there must be something wrong that makes the fake creation go wrong. Is the "UserDocument"-type public? If it is internal and you have given your test-project access to it through the use of InternalsVisibleToAttribute you must give the proxy generating library access to it as well: https://github.com/FakeItEasy/FakeItEasy/wiki/How-to-fake-internal-(Friend-in-VB)-types.
这篇关于伪造/嘲讽的界面给人"没有默认构造函数"错误,怎么可能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!