0

I'm creating an API to interact with a basic home router. In this case the only way this is possible is with a WebBrowser object. I put that object in a BrowserWrapper class to hide most of the complexity.

Here is my XUnit [Fact] for retrieving the SSID.

[Fact]
public void GetSsid()
{
   RouterApi api = new RouterApi();
   string ssid = api.GetSsid();
   Assert.NotNull(ssid);
}

Constructor for RouterApi

private BrowserWrapper browserWrapper;

public RouterApi()
{
   try
   {
      browserWrapper = new BrowserWrapper();

      Thread browserThread = new Thread(() => { Application.Run(browserWrapper); });
      browserThread.SetApartmentState(ApartmentState.STA);
      browserThread.Start();
   }
   catch (Exception ex) { //Handle ex }
}

I know the BrowserWrapper constructor isn't being called because in the constructor for BrowserWrapper I instantiate a bunch of things (one of them is the WebBrowser control), and they're still null when I try to interact with them in api.GetSsid(). (Note: I'm using delegate methods to interact with my BrowserWrapper from my RouterApi.)

I know this all works because I've already written an API almost exactly this way, but this is my first time using XUnit, so I think the problem lies there.

user3112658
  • 121
  • 2
  • 8
  • 2
    The constructor is called *before* you start the thread. That's not what you want. Use [code like this](http://stackoverflow.com/a/4271581/17034). – Hans Passant Apr 24 '15 at 18:01
  • So I should move where I instantiate BrowserWrapper? BrowserWrapper isn't my WebBrowser, it just contains one. – user3112658 Apr 24 '15 at 18:55

0 Answers0