我怎样才能让这个假货工作?我希望最后一个断言会通过。
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?
}
}
}
在
初始设置后添加行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());
}
}
}