我能够模拟会话但是我无法成功模拟sessionconfiguration类。如果您在本文的底部看到我的Unit测试类,则调用我的类的connect方法。在connect方法中调试Settings对象时始终为null。你能让我知道可能存在什么问题
我有一个名为XYZ的课程。它在其中声明了名为
的FixSessionConfiguration依赖项Class XYZ
{
[Dependency]
public FixSessionConfiguration Settings
{
get;
set;
}
public void Connect()
{
iFixSession = new FixSession(
Settings.SenderCompID,
Settings.TargetCompID,
Settings.Version,
Settings.SetResetSeqNumFlag);
}
}
我基本上需要为我的单元测试模拟FixSessionConfiguration类。为此,我为会话配置创建了接口。这是代码
public class FixSessionConfiguration : SessionConfiguration,ISessionConfiguration
{
[ConfigurationProperty("senderSubID")]
public string SenderSubID
{
get { return (string)this["senderSubID"]; }
set { this["senderSubID"] = value; }
}
[ConfigurationProperty("senderLocationID")]
public string SenderLocationID
{
get { return (string)this["senderLocationID"]; }
set { this["senderLocationID"] = value; }
}
[ConfigurationProperty("targetSubID")]
public string TargetSubID
{
get { return (string)this["targetSubID"]; }
set { this["targetSubID"] = value; }
}
public new string SenderCompID { get; set; }
public new string TargetCompID { get; set; }
ProtocolVersion Version { get; set; }
public new bool SetResetSeqNumFlag { get; set; }
public new string Host { get; set; }
public new int Port { get; set; }
}
SessionConfiguration类,它是第三方二进制文件的一部分。
public class SessionConfiguration : ConfigurationSection
{
[ConfigurationProperty("host", IsRequired = true)]
public string Host
{
get { return (string)this["host"]; }
set { this["host"] = value; }
}
[ConfigurationProperty("port", IsRequired = true)]
public int Port
{
get { return (int)this["port"]; }
set { this["port"] = value; }
}
[ConfigurationProperty("senderCompID", IsRequired = true)]
public string SenderCompID
{
get { return (string)this["senderCompID"]; }
set { this["senderCompID"] = value; }
}
[ConfigurationProperty("targetCompID", IsRequired = true)]
public string TargetCompID
{
get { return (string)this["targetCompID"]; }
set { this["targetCompID"] = value; }
}
[ConfigurationProperty("fixVersion", IsRequired = true)]
public ProtocolVersion Version
{
get
{
return (ProtocolVersion)this["fixVersion"];
}
set { this["fixVersion"] = value; }
}
[ConfigurationProperty("keepSequenceNumbersAfterLogout", IsRequired = true)]
public bool KeepSequenceNumbersAfterLogout
{
get { return (bool)this["keepSequenceNumbersAfterLogout"]; }
set { this["keepSequenceNumbersAfterLogout"] = value; }
}
}
我为SessionConfiguration类创建的接口。
public interface ISessionConfiguration
{
string SenderCompID { get; set; }
string TargetCompID { get; set; }
ProtocolVersion Version { get; set; }
bool SetResetSeqNumFlag { get; set; }
string Host { get; set; }
int Port { get; set; }
}
我的单元测试课
Class UnitTest
{
private Mock<ISessionConfiguration> _mockSessionConfiguration;
[Fact]
public void Buy_QuoteAndDeal()
{
_mockSessionConfiguration = new Mock<ISessionConfiguration>();
_mockSessionConfiguration.Setup(x => x.SenderCompID).Returns("BESTINVESTLON");
_mockSessionConfiguration.Setup(x => x.TargetCompID).Returns("WINS");
_mockSessionConfiguration.Setup(x => x.Version).Returns(ProtocolVersion.FIX42);
_mockSessionConfiguration.Setup(x => x.SetResetSeqNumFlag).Returns(true);
_mockSessionConfiguration.Setup(x => x.Host).Returns("localhost");
_mockSessionConfiguration.Setup(x => x.Port).Returns(4500);
shareDealingEngine.Connect();
}
}
答案 0 :(得分:1)
由于类属性是FixSessionConfiguration而不是ISessionConfiguration,因此您的模拟工作无法正常工作。由于FixSessionConfiguration没有虚拟属性,你也不能嘲笑它。
你真的应该将XYZ属性从FixSessionConfiguration更改为ISessionConfiguration - 这将允许你嘲笑它 - 但是你的测试仍然无法正常工作,因为你还没有将你的模拟传递到你的课程中。您需要执行以下操作:
var myClass = new XYZ();
myClass.Settings = _mockSessionConfiguration.Object;
// insert test code here
同样,理想情况下,设置会通过构造函数传递,但这与您所面临的问题并不完全相关。