当对象的默认构造函数不为空时无法创建伪造
Failed to create fake when default constructor for the object is not empty
我是 FakeItEasy 的新手,我有一些以前用来通过的测试用例(当我没有定义特定的构造函数时)。
然后我为我伪造的对象创建了一个默认构造函数,默认构造函数只创建记录器。
创建此构造函数后,我的所有测试用例现在都失败了,并出现以下错误,我无法弄清楚它不喜欢什么或如何修复它:
FakeItEasy.Core.FakeCreationException :
Failed to create fake of type StorageService.
Below is a list of reasons for failure per attempted constructor:
No constructor arguments failed:
No usable default constructor was found on the type StorageService.
An exception of type System.NullReferenceException was caught during this call. Its message was:
Object reference not set to an instance of an object.
这是我的 class:
public class StorageService : IStorageService
{
public StorageService()
{
Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().Enrich.FromLogContext().WriteTo.Loggly(
logglyConfig: new LogglyConfiguration
{
//Log Config is here
}).CreateLogger();
}
public async Task<bool> CreateTableIfNotExistsAsync(string storageConnectionString, string tableName)
{
// Some code here
return result;
}
}
这是我的测试用例:
[Test]
public void create_storage()
{
FakedStorageService = A.Fake<StorageService>(); // It is failing at this line
// Some FakeCalls here
}
我认为问题出在您试图创建伪造的实现而不是接口。你可以修改成这样:
FakedStorageService = A.Fake<IStorageService>();
我是 FakeItEasy 的新手,我有一些以前用来通过的测试用例(当我没有定义特定的构造函数时)。 然后我为我伪造的对象创建了一个默认构造函数,默认构造函数只创建记录器。
创建此构造函数后,我的所有测试用例现在都失败了,并出现以下错误,我无法弄清楚它不喜欢什么或如何修复它:
FakeItEasy.Core.FakeCreationException :
Failed to create fake of type StorageService.
Below is a list of reasons for failure per attempted constructor:
No constructor arguments failed:
No usable default constructor was found on the type StorageService.
An exception of type System.NullReferenceException was caught during this call. Its message was:
Object reference not set to an instance of an object.
这是我的 class:
public class StorageService : IStorageService
{
public StorageService()
{
Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().Enrich.FromLogContext().WriteTo.Loggly(
logglyConfig: new LogglyConfiguration
{
//Log Config is here
}).CreateLogger();
}
public async Task<bool> CreateTableIfNotExistsAsync(string storageConnectionString, string tableName)
{
// Some code here
return result;
}
}
这是我的测试用例:
[Test]
public void create_storage()
{
FakedStorageService = A.Fake<StorageService>(); // It is failing at this line
// Some FakeCalls here
}
我认为问题出在您试图创建伪造的实现而不是接口。你可以修改成这样:
FakedStorageService = A.Fake<IStorageService>();