问题描述
我只是试图让在Visual Studio 2012 RC熟悉新的正版正货分离框架,但我因此与 ShimNotSupportedException
取值面临的问题。
结果在第一次尝试,我试图挂钩委托每个单独的垫片的方法来,只好抛出试图运行/调试测试时, ShimNotSupportedException
[TestMethod的]
公共无效GetFoo_ValidBar_ReturnsBaz()
{
按(ShimsContext.Create())
{
ShimDateTime.NowGet =()=>新的DateTime(2012,08,11,10,20,59);
常量字符串预期=20120811_102059;
串实际=的getFoo();
Assert.AreEqual(预期,实际值);
}
}
这是对应的堆栈跟踪:
After having read the two threads I had found at MSDN dealing with this issue I followed their instructions (turning CodeCoverage off, deleting .testsettings file) which didn't work for me!
Nevertheless I have found a workaround for this issue:
By firstly running all tests from the Test Explorer (instead of using the "MSTest Test (click to run)" button directly out of the coding area) everything worked correctly and no exceptions were thrown. Afterwards I could even debug the test and the assignment to the shim method worked just as expected.
This worked for all following shims I used as well.
But now I'm having the same issue again when trying to implement fakes of the MS Enterprise Library for database access.
This is what the test looks like:
[TestMethod]
public void GetFooFromEF_NonEmptyDataReader_ObjectsCorrectlyInstantiated()
{
using(ShimsContext.Create()){
var dataReader = new StubIDataReader()
{
ItemGetString = s => 1,
DepthGet = () => 2
};
ShimFoo.GetBar = guid => dataReader;
var bar = new StubIBar()
{
ConvertIBarToBaz = record => null
};
ShimQux.AllInstances.GetBar = (a, b) => bar;
var dbFactory = new StubDbProviderFactory();
var db = new StubDatabase("test", dbFactory);
ShimDatabaseFactory.CreateDatabaseString = s => db;
List<BarInformation> actual = accessor.InvokeStatic("GetBar",
new Object[] { }) as List<BarInformation>;
Assert.IsTrue(true);
}
}
The first two shim assignments (ShimFoo & ShimQux) are working as expected. But ShimDatabaseFactory.CreateDatabaseString (which is supposed to make DatabaseFactory.CreateDatabase(string) return a stub database when trying to create a new database instance) throws a ShimNotSupportedException again. And I just can't figure out why!
Do you have any ideas what went wrong here?
I would appreciate any input on this.
Thanks,
Benjamin
I had the same exact problem. Try to remove all testsettings files (both from disk and solution), and make sure your solution does not reference any testsettings files.
Also make sure you're using the visual studio testrunner (and not resharper etc. which is instrumenting the code).
I've written two blogposts about these issues which may be helpful:
Visual Studio 2012 Fakes – ShimNotSupportedException when debugging tests
Unit testing – Visual Studio 2012 Fakes in Team City
这篇关于ShimNotSupportedException在MS的VisualStudio 2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!