再見,
我的 NuGet 包具有這種結構的一個解決方案:
root
Directory.Build.props
MyNuGetPackage1
NuGetClass.cs
MyNuGetPackage1.Tests
MyNuGet.sln
NuGetClass.cs類具有以下代碼:
public class Classe
{
internal Classe()
{
?
}
?
public int Id { get; set; }
}
在不同解決方案的其他測驗專案中,我參考了MyNuGetPackage1,我需要這樣做:
public class MyTests
{
Fixture fixture = new Fixture();
[Test]
public void MyTest()
{
fixture.Build<Classe>().Create(); // This code throw exception, see below for details
}
}
AutoFixture.ObjectCreationExceptionWithPath :AutoFixture 無法從 AutoFixture.Kernel.SededRequest 創建實體,因為創建意外失敗并出現例外。請參閱內部例外以調查失敗的根本原因。
請求路徑:...
內部例外訊息:AutoFixture.ObjectCreationException:裝飾的 ISpecimenBuilder 無法根據請求創建樣本:Classe。如果請求代表一個介面或抽象類,就會發生這種情況;如果是這種情況,請注冊一個可以根據請求創建樣本的 ISpecimenBuilder。如果這發生在強型別構建運算式中,請嘗試使用 IFactoryComposer 方法之一提供工廠。
我嘗試使用以下配置更新我的Directory.Build.props:
<?xml version="1.0" encoding="utf-8"?>
<Project>
...
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(MSBuildProjectName).Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
...
</Project>
但此配置僅適用于 nuget 解決方案的測驗專案。我應該避免為每個專案手動添加InternalsVisibleTo,如下所示:
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>DestinationProject.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
我怎樣才能實作我的目標?
[編輯]
我已經嘗試了這兩種方法但沒有成功:
情況1
[Test]
public void MyTest()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var classeMock = fixture.Freeze<Mock<Classe>>();
fixture.Build<Classe>().Create(); // this code throw exception, see below for details
}
裝飾的 ISpecimenBuilder 無法根據以下請求創建樣本:Classe。如果請求代表一個介面或抽象類,就會發生這種情況;如果是這種情況,請注冊一個可以根據請求創建樣本的 ISpecimenBuilder。如果這發生在強型別構建運算式中,請嘗試使用 IFactoryComposer 方法之一提供工廠。
案例二
[Test]
public void MyTest()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var classeMock = fixture.Freeze<Mock<Classe>>();
fixture.Build<Mock<Classe>>().Create(); // this code throw exception, see below for details
}
無法實體化類的代理:Classe。找不到無引數建構式。
謝謝
uj5u.com熱心網友回復:
嘗試使用這種方法,其中使用對 Moq 可見的內部,而不是您的測驗專案:https : //stackoverflow.com/a/17574183/186861
而且你不能用 Fixture.Create 實體化抽象類
試試這個:
[Test]
public void MyTest()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var classeMock = fixture.Freeze<Mock<Classe>>();
// setup classeMock with Moq and test
}
示例:https : //autofixture.github.io/docs/quick-start/#
uj5u.com熱心網友回復:
默認情況下,AutoFixture 不適用于非公共建構式。這也包括internal建構式。
要么宣告一個公共(非復制)建構式,要么指示 AutoFixture 它應該如何創建類實體。
fixture.Customize<MyClass>(
c => c.FromFactory(
() => /* Create your object here. */));
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/408315.html
標籤:
