我正在尝试编写一个涵盖以下行的单元测试
var fileFullName = fileInfo.FullName;
Run Code Online (Sandbox Code Playgroud)
其中fileInfo是FileInfo的一个实例.
我使用fakes来填充FileInfo对象,但是我无法为FullName属性提供值,因为它是从基类继承的.
对于未继承的Name属性,我可以这样做:
ShimFileInfo.AllInstances.NameGet = info => OriginalFullName;
Run Code Online (Sandbox Code Playgroud)
Microsoft提供的答案是在基类上创建填充程序,在本例中为FileSystemInfo.但如果我试试这个:
ShimFileSystemInfo.AllInstances.FullNameGet = info => OriginalFullName;
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为FileSystemInfo是一个无法创建的抽象类,因此无法进行填充.
在这种特殊情况下,我可以解决它,因为我可以结合DirectoryName和Name属性使其可测试,但似乎很疯狂,我不能只使用我想要的属性,因为它恰好来自基地.
有没有人来解决这个问题并设法解决它?
所以我对它的实现方式感兴趣.基本上:我怎样才能自己重新实现同样的事情?......我不打算这样做.只是了解.
根本问题是:(?)如何拦截类实例化?如何在一个实例中替换它,在另一个实现中替换它的原始或甚至不同的实现?
如何拦截静态方法或密封类甚至是可能的.
这与垫片/痣有关,而不是存根.
我在测试项目的参考'快捷菜单中看不到"添加伪装配"项.Visual Studio社区2013版中是否提供Microsoft Fakes Framework?
嗨,我正在尝试运行一个简单的测试,取代每个的Features.Count SPSite,但我不能.你能帮助我吗?
我有这个代码:
[TestMethod]
public void AllInstances()
{
using (var context = new SPEmulationContext(IsolationLevel.Fake))
{
ShimSPSite.AllInstances.FeaturesGet = (item) =>
{
return new ShimSPFeatureCollection()
{
CountGet = () => 1
}.Instance;
};
var tSite = new SPSite("http://fakesite");
Assert.AreEqual(1, tSite.Features.Count);
}
}
Run Code Online (Sandbox Code Playgroud)
但它失败了这个例外:
Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotImplementedException:抛出了类型'Microsoft.QualityTools.Testing.Fakes.Shims.ShimNotImplementedException'的异常.
这个类似的工作正常
[TestMethod]
public void Shim()
{
using (var context = new SPEmulationContext(IsolationLevel.Fake))
{
var tSite = new ShimSPSite()
{
FeaturesGet = () =>
{
return new ShimSPFeatureCollection()
{
CountGet = () => 1
}.Instance;
}
}.Instance;
Assert.AreEqual(1, …Run Code Online (Sandbox Code Playgroud) 我安装了VS2015 RTM和VS2013 Update 5 RTM.现在我的解决方案没有构建,因为我有一个具有返回类型a的接口X509Certificate2.现在我的假货不是建造的.我还创建了一个测试项目,我遇到了同样的问题,所以这不是我的解决方案.我收到的消息是:
无法为ClassLibrary1.Interfaces.ICertificateProvider生成存根:方法System.Security.Cryptography.X509Certificates.X509Certificate2 ClassLibrary1.Interfaces.ICertificateProvider.getbla()unstubbable:方法是抽象的,无法存根,键入System.Security.Cryptography.X509Certificates.X509Certificate2在目标框架版本中不可用.
现在我卸载了VS2015 RTM,但问题仍然存在.当我用返回类型注释掉该方法时,证书一切正常.当我取消注释时,问题出在那里.
更新1 我刚在另一个系统上测试了这个.首先,我尝试使用VS2013 Update 4和VS2015 RC.有了这个设置一切都很好.然后我在该系统上安装了Update 5 RTM,然后它不再工作了.所以Update 5一定是问题所在! 结束更新
重现:使用.Net Framework 4.5.1创建包含2个类库和1个测试项目的解决方案.在类库1中创建一个接口.
namespace ClassLibrary1.Interfaces
{
public interface ICertificateProvider
{
// Comment this line so you can build your fakes assembly...
X509Certificate2 getbla();
}
}
Run Code Online (Sandbox Code Playgroud)
在第二个类库中创建一个类.实现接口并添加对第一个类库的引用.
namespace ClassLibrary1
{
public class CertificateProvider : ClassLibrary1.Interfaces.ICertificateProvider
{
public X509Certificate2 getbla()
{
throw new NotImplementedException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
为unittest项目中的接口项目添加fakes程序集.在测试中通过以下代码:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary1.Interfaces.Fakes;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{ …Run Code Online (Sandbox Code Playgroud) 一段时间以前就已经提出了一个几乎相似的问题但尚未回答.由于我的设置略有不同,我开始尝试新的尝试:
我演示了我的代码的缩短版本,但即使是这个简短的版本也会产生错误.
我有一个方法返回一个实例System.Printing.LocalPrintServer:
public class PrintServerProvider
{
public LocalPrintServer Provide()
{
return new LocalPrintServer(new string[0], PrintSystemDesiredAccess.EnumerateServer);
}
}
Run Code Online (Sandbox Code Playgroud)
对于这种方法,我使用垫片实现了单元测试:
[TestClass]
public class PrintServerProviderTests
{
[TestMethod]
public void Provide_Success()
{
using (ShimsContext.Create())
{
bool constructorCalled = false;
ShimLocalPrintServer.ConstructorStringArrayPrintSystemDesiredAccess = (instance, props, access) =>
{
constructorCalled = true;
};
PrintServerProvider provider = new PrintServerProvider();
LocalPrintServer server = provider.Provide();
Assert.IsNotNull(server);
Assert.IsTrue(constructorCalled);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Visual Studio 2013中,此测试工作正常.
但是,在Visual Studio 2015年(更新一),当被测试的方法调用new LocalPrintServer(...)的
System.Printing.LocalPrintServer..ctor(String [] …
c# visual-studio invalidprogramexception microsoft-fakes visual-studio-2015
我正在尝试为一个项目设置自动构建和单元测试,该项目使用Fakes库进行单元测试.该项目在我的Windows 10 PC(安装了VS 2017 Enterprise)上构建和测试很好,但是使用相同的命令在构建服务器(也是VS 2017 Enterprise的Windows 10)上编译项目会产生关于Fakes not existing的几个错误.确切的错误如下所示:
XControllerTests.cs(10,20): error CS0234: The type or namespace 'Fakes' does not exist in the namespace 'System.Data.Common' (are you missing an assembly reference?) [C:\Runner\builds\xxx\XTests.csproj]
根据我的研究,这是由使用旧版本的MSBuild引起的,但是我检查了服务器,并确认它已安装了visual studio的最新版本和更新.我还确认构建脚本使用的是正确版本的MSBuild.exe,即c:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe.在我的桌面上使用此版本正确编译项目.
为什么构建不能在(相同的设置)构建服务器上运行?
在 .Net Core 中使用 xUnit 和 Moq 编写测试用例时遇到突出问题
我已经使用 MSTest Fakes 在下面编写了测试用例。它按预期工作正常。
[TestClass]
public class TestBlobServiceProvider
{
private Common.Interfaces.IBlobServiceProvider _iblobprovider;
public TestBlobServiceProvider()
{
Common.Interfaces.IBlobServiceProvider iblobprovider = new BlobServiceProvider();
this._iblobprovider = iblobprovider;
}
public TestBlobServiceProvider(string storageConnectionString)
{
}
[TestMethod]
public void Move_Success()
{
using (ShimsContext.Create())
{
string sourceContainer = "a";
string destinationContainer = "s";
string sourceFileName = "d";
string destinationFileName = "e";
Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob sourceFile = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob ();
Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob destFile = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob();
Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob.AllInstances.StartCopyCloudBlockBlobAccessConditionAccessConditionBlobRequestOptionsOperationContext = (x, y, z, d, e,s) => …Run Code Online (Sandbox Code Playgroud) 我正在尝试使Microsoft Fakes与我的单元测试项目(net47)一起正常工作,其中我的项目文件格式正在使用新的NetSDK格式。
使用Visual Studio 2019,我可以添加一个Fakes程序集,并且看起来一切正常,直到我们尝试在构建代理上构建/运行测试为止。看起来,当您编译/构建项目时,会生成伪造的程序集,但它们是并行执行的,或者是在构建后执行的(我不确定)。在运行Visual Studio 2019 Enterprise的开发计算机上也会发生相同的问题。
我注意到,如果不存在FakesAssemblies文件夹,则在构建过程中会创建一个文件夹,但是编译会失败,因为没有发现* .Fakes命名空间。第二个编译/构建有效,因为现在已填充FakesAssemblies文件夹。需要注意的一件事是,我将Microsoft Fakes的dll文件放入了我们公司私人提要中的NuGet包中,这样我就可以将其作为NuGet包而不是引用来拉下来。
SampleUnitTests.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="Microsoft.QualityTools.Testing.Fakes" Version="16.0.28621.142" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>
<ItemGroup>
<Fakes Include="Fakes\*.fakes" />
<Reference Include="FakesAssemblies\*.dll" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
我已经在许多网站上找到了这个问题的各种迭代,但到目前为止,我还没有找到任何提供完整答案的东西.我刚刚设置了许多Visual Studio测试代理,这些代理似乎都在运行,并且正在运行我们计划在其上运行的大多数单元测试.然而,当我来到检查单元测试中,它未能就行之一:
using (ShimsContext.Create()),
随着堆栈跟踪:
看了我安装了Visual Studio 2013的测试代理一些其他职位,并证实将在本地运行测试.然后我将COR_PROFILER_PATH设置为我的机器上使用的分析器,该分析器安装在:"C:\ Program Files\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\IntelliTrace\12.0.0\Microsoft.IntelliTrace.Profiler .12.0.0.dll".然后它在跟踪的同一个地方失败了:
建议是在一个单独的进程下加载了探查器而无法使用.有没有人在这个或类似的设置上取得任何成功?在基础层面上,Visual Studio Test Agents可以使用Fakes运行测试吗?
谢谢
Result Message:
Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException: Failed to resolve profiler path from COR_PROFILER_PATH and COR_PROFILER environment variables.
Test method threw exception:
Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException: Failed to get profiler module handle 'C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\IntelliTrace\12.0.0\Microsoft.IntelliTrace.Profiler.12.0.0.dll'. The specified module could not be found ---> System.ComponentModel.Win32Exception: The specified module could not be found
unit-testing visual-studio microsoft-fakes visual-studio-2013
microsoft-fakes ×10
c# ×5
.net ×3
shim ×3
unit-testing ×3
msbuild ×2
.net-core ×1
csproj ×1
inheritance ×1
moles ×1
moq ×1
mscorlib ×1
sharepoint ×1
system ×1
xunit ×1