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.