public void MyTestInitialize() { // Setup Logger logger = new Mock <Logger>(); Factory.mockLogger = logger.Object; // create file directory for log files string bp = AppDomain.CurrentDomain.BaseDirectory.ToString(); Directory.CreateDirectory(bp); string bp2 = Path.Combine(bp, "bin"); Directory.CreateDirectory(bp2); // Setup IBClient mockIBClient = new Mock <IBClient>(); Factory.mockIBClient = mockIBClient.Object; // make ibclient.IConnected work correctly bool isConnected = false; mockIBClient.Setup(ib => ib.Connected).Returns(() => isConnected); mockIBClient.Setup(ib => ib.Connect("MyTestHost", 6543, 9876)).Callback(() => isConnected = true); // other calls mockIBClient.Setup(ib => ib.ServerVersion).Returns(42); mockIBClient.Setup(ib => ib.TwsConnectionTime).Returns("03:04:05 2011-11-08"); // Invoke ibclient Events (Callbacks) mockIBClient.Setup(ib => ib.RequestIds(1)) .Raises(ib => ib.NextValidId += null, new NextValidIdEventArgs(42)); mockIBClient.Setup(ib => ib.RequestManagedAccts()) .Raises(ib => ib.ManagedAccounts += null, new ManagedAccountsEventArgs("U111,U222")); // mockIBClient.Setup(ib => ib.Disconnect()).Callback(() => isConnected = false); // does not work... mockIBClient.Setup(ib => ib.Disconnect()).Raises(ib => ib.ConnectionClosed += null, new ConnectionClosedEventArgs()); // Setup UserProvider userProvider = new Mock <UserProviderWrapper>(); Factory.userProviderWrapper = userProvider.Object; // Connect StingrayOQ target = new StingrayOQ_Accessor(); target.Hostname = "MyTestHost"; target.ClientId = 9876; // used for all orders target.Port = 6543; target.Connect(); }
public void ConnectTest() { Mock<IBClient> mockIBClient = new Mock<IBClient>(); Factory.mockIBClient = mockIBClient.Object; Mock<UserProviderWrapper> userProvider = new Mock<UserProviderWrapper>(); Factory.userProviderWrapper = userProvider.Object; // make ibclient.IConnected work correctly bool isConnected = false; mockIBClient.Setup(ib => ib.Connected).Returns(() => isConnected); mockIBClient.Setup(ib => ib.Connect("MyTestHost", 6543, 9876)).Callback(() => isConnected = true); // other calls mockIBClient.Setup(ib => ib.ServerVersion).Returns(42); mockIBClient.Setup(ib => ib.TwsConnectionTime).Returns("03:04:05 2011-11-08"); // Invoke ibclient Events (Callbacks) mockIBClient.Setup(ib => ib.RequestManagedAccts()) .Raises(ib => ib.ManagedAccounts += null, new ManagedAccountsEventArgs("U111,U222")); StingrayOQ_Accessor target = new StingrayOQ_Accessor(); target.Hostname = "MyTestHost"; target.ClientId = 9876; target.Port = 6543; target.Connect(); // Act logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("START.* Initialize.*"))); logger.Verify(log => log.AddLog(LoggerLevel.Information, It.IsRegex("=== StingrayOQ V .* started ==="))); // CheckTWS logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("START.* CheckTWS.*"))); // openConnection logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("START.* openConnection.*"))); logger.Verify(log => log.AddLog(LoggerLevel.Detail, "new IBClient")); logger.Verify(log => log.AddLog(LoggerLevel.Detail, "Connect")); mockIBClient.Verify(ib => ib.Connect("MyTestHost", 6543, 9876)); mockIBClient.Verify(ib => ib.RequestManagedAccts()); logger.Verify(log => log.AddLog(LoggerLevel.Information, It.IsRegex("2 Accounts found"))); // RequestAccount Updates mockIBClient.Verify(ib => ib.RequestAccountUpdates(true, "U111")); mockIBClient.Verify(ib => ib.RequestAccountUpdates(true, "U222")); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("Request account updates for U111"))); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("Request account updates for U222"))); // Request Open Orders mockIBClient.Verify(ib => ib.RequestOpenOrders()); // Request IDs mockIBClient.Verify(ib => ib.RequestIds(1)); logger.Verify( log => log.AddLog(LoggerLevel.Information, It.IsRegex("Connected, Server Version = 42, Connection Time = 03:04:05 2011-11-08"))); // EmitConncted userProvider.Verify(up => up.EmitConnected()); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("END.* openConnection.*"))); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("END.* CheckTWS.*"))); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("END.* Initialize.*"))); // never saw an error? logger.Verify(log => log.AddLog(LoggerLevel.Error, It.IsAny<string>()), Times.Never(), "unexpected error log"); // State Assert.IsTrue(target.IsConnected, "IsConnected"); Assert.IsTrue(target.TWSConnected, "TWSConnected"); }
public void ConnectTest() { Mock <IBClient> mockIBClient = new Mock <IBClient>(); Factory.mockIBClient = mockIBClient.Object; Mock <UserProviderWrapper> userProvider = new Mock <UserProviderWrapper>(); Factory.userProviderWrapper = userProvider.Object; // make ibclient.IConnected work correctly bool isConnected = false; mockIBClient.Setup(ib => ib.Connected).Returns(() => isConnected); mockIBClient.Setup(ib => ib.Connect("MyTestHost", 6543, 9876)).Callback(() => isConnected = true); // other calls mockIBClient.Setup(ib => ib.ServerVersion).Returns(42); mockIBClient.Setup(ib => ib.TwsConnectionTime).Returns("03:04:05 2011-11-08"); // Invoke ibclient Events (Callbacks) mockIBClient.Setup(ib => ib.RequestManagedAccts()) .Raises(ib => ib.ManagedAccounts += null, new ManagedAccountsEventArgs("U111,U222")); StingrayOQ_Accessor target = new StingrayOQ_Accessor(); target.Hostname = "MyTestHost"; target.ClientId = 9876; target.Port = 6543; target.Connect(); // Act logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("START.* Initialize.*"))); logger.Verify(log => log.AddLog(LoggerLevel.Information, It.IsRegex("=== StingrayOQ V .* started ==="))); // CheckTWS logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("START.* CheckTWS.*"))); // openConnection logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("START.* openConnection.*"))); logger.Verify(log => log.AddLog(LoggerLevel.Detail, "new IBClient")); logger.Verify(log => log.AddLog(LoggerLevel.Detail, "Connect")); mockIBClient.Verify(ib => ib.Connect("MyTestHost", 6543, 9876)); mockIBClient.Verify(ib => ib.RequestManagedAccts()); logger.Verify(log => log.AddLog(LoggerLevel.Information, It.IsRegex("2 Accounts found"))); // RequestAccount Updates mockIBClient.Verify(ib => ib.RequestAccountUpdates(true, "U111")); mockIBClient.Verify(ib => ib.RequestAccountUpdates(true, "U222")); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("Request account updates for U111"))); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("Request account updates for U222"))); // Request Open Orders mockIBClient.Verify(ib => ib.RequestOpenOrders()); // Request IDs mockIBClient.Verify(ib => ib.RequestIds(1)); logger.Verify( log => log.AddLog(LoggerLevel.Information, It.IsRegex("Connected, Server Version = 42, Connection Time = 03:04:05 2011-11-08"))); // EmitConncted userProvider.Verify(up => up.EmitConnected()); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("END.* openConnection.*"))); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("END.* CheckTWS.*"))); logger.Verify(log => log.AddLog(LoggerLevel.Detail, It.IsRegex("END.* Initialize.*"))); // never saw an error? logger.Verify(log => log.AddLog(LoggerLevel.Error, It.IsAny <string>()), Times.Never(), "unexpected error log"); // State Assert.IsTrue(target.IsConnected, "IsConnected"); Assert.IsTrue(target.TWSConnected, "TWSConnected"); }
public void MyTestInitialize() { // Setup Logger logger = new Mock<Logger>(); Factory.mockLogger = logger.Object; // create file directory for log files string bp = AppDomain.CurrentDomain.BaseDirectory.ToString(); Directory.CreateDirectory(bp); string bp2 = Path.Combine(bp, "bin"); Directory.CreateDirectory(bp2); // Setup IBClient mockIBClient = new Mock<IBClient>(); Factory.mockIBClient = mockIBClient.Object; // make ibclient.IConnected work correctly bool isConnected = false; mockIBClient.Setup(ib => ib.Connected).Returns(() => isConnected); mockIBClient.Setup(ib => ib.Connect("MyTestHost", 6543, 9876)).Callback(() => isConnected = true); // other calls mockIBClient.Setup(ib => ib.ServerVersion).Returns(42); mockIBClient.Setup(ib => ib.TwsConnectionTime).Returns("03:04:05 2011-11-08"); // Invoke ibclient Events (Callbacks) mockIBClient.Setup(ib => ib.RequestIds(1)) .Raises(ib => ib.NextValidId += null, new NextValidIdEventArgs(42)); mockIBClient.Setup(ib => ib.RequestManagedAccts()) .Raises(ib => ib.ManagedAccounts += null, new ManagedAccountsEventArgs("U111,U222")); // mockIBClient.Setup(ib => ib.Disconnect()).Callback(() => isConnected = false); // does not work... mockIBClient.Setup(ib => ib.Disconnect()).Raises(ib => ib.ConnectionClosed += null, new ConnectionClosedEventArgs()); // Setup UserProvider userProvider = new Mock<UserProviderWrapper>(); Factory.userProviderWrapper = userProvider.Object; // Connect StingrayOQ target = new StingrayOQ_Accessor(); target.Hostname = "MyTestHost"; target.ClientId = 9876; // used for all orders target.Port = 6543; target.Connect(); }