我能夠模擬會話,但我無法成功模擬會話配置類。如果你看到我的單元測試類在這篇文章的底部調用我的類的連接方法。在connect方法中調試Settings對象時始終爲空。你能讓我知道什麼可能是問題C#中的嘲笑依賴不工作
我有一個名爲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();
}
}