我怎么才能让这个假货起作用呢?我希望最后一个断言能通过。
System.Data.fakes
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
<Assembly Name="System.Data" Version="4.0.0.0"/>
</Fakes>Test.cs
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlClient.Fakes;
using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class FakeTest
{
[TestMethod]
public void DownCast()
{
using (ShimsContext.Create())
{
SqlConnection sqlCn = new ShimSqlConnection
{
CreateCommand = () => new ShimSqlCommand(),
CreateDbCommand = () => new ShimSqlCommand()
};
Assert.IsNotNull(sqlCn.CreateCommand());
DbConnection dbCn = sqlCn;
Assert.IsNotNull(dbCn.CreateCommand()); // How can I make this pass?
}
}
}发布于 2017-10-13 01:45:45
在初始设置之后添加new ShimDbConnection(sqlCn) { CreateCommand = () => new ShimSqlCommand() };行将允许测试通过。
using System.Data.Common;
using System.Data.Common.Fakes;
using System.Data.SqlClient;
using System.Data.SqlClient.Fakes;
using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class FakeTest
{
[TestMethod]
public void DownCast()
{
using (ShimsContext.Create())
{
SqlConnection sqlCn = new ShimSqlConnection
{
CreateCommand = () => new ShimSqlCommand(),
CreateDbCommand = () => new ShimSqlCommand()
};
new ShimDbConnection(sqlCn) { CreateCommand = () => new ShimSqlCommand() }; // Adding this line, the test passes.
Assert.IsNotNull(sqlCn.CreateCommand());
DbConnection dbCn = sqlCn;
Assert.IsNotNull(dbCn.CreateCommand());
}
}
}https://stackoverflow.com/questions/46715121
复制相似问题