Reputation: 8681
I am able to mock the session however I am not able to successfully mock the sessionconfiguration class. If you see my Unit test class at the bottom of this post calls the connect method of my class. While debugging the Settings object in the connect method is always null. could you let me know what may be problem
I have a class called XYZ. It has FixSessionConfiguration dependency declared in it called
Class XYZ
{
[Dependency]
public FixSessionConfiguration Settings
{
get;
set;
}
public void Connect()
{
iFixSession = new FixSession(
Settings.SenderCompID,
Settings.TargetCompID,
Settings.Version,
Settings.SetResetSeqNumFlag);
}
}
I basically need to mock FixSessionConfiguration class for my unit test. To do that I created interface for Session Configuration. Here is the code
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; }
}
The SessionConfiguration class which is part of third party binary.
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; }
}
}
The interface that I created for SessionConfiguration class.
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; }
}
My unit test class
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();
}
}
Upvotes: 1
Views: 72
Reputation: 956
As the class property is a FixSessionConfiguration not an ISessionConfiguration, your mock won't work. As FixSessionConfiguration doesn't have virtual properties, you can't mock that either, really.
Really you should change the XYZ property from FixSessionConfiguration to ISessionConfiguration - that will allow you to mock it out - but your test still won't work as you haven't passed your mock into your class yet. You'll need to do something like:
var myClass = new XYZ();
myClass.Settings = _mockSessionConfiguration.Object;
// insert test code here
Again, ideally Settings would be passed in via the constructor, but that's not really related to the issues you're facing.
Upvotes: 1