Unit test initialization fails with WebBrowser control (COMException)

576 views Asked by At

Message: Initialization method MyProject.Tests.MainWindow.Initialize threw exception. System.Runtime.InteropServices.COMException:
System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component..

StackTrace:

  • System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.get_RegisterAsDropTarget()
  • System.Windows.Forms.WebBrowser.set_AllowWebBrowserDrop(Boolean value)
  • MyProject.MainWindow.InitializeComponent() in MainWindow.Designer.cs:line 819
  • MyProject.MainWindow.InitWindow() in MainWindow.cs:line 29
  • MyProject.MainWindow..ctor() in MainWindow.cs:line 19
  • MyProject.Tests.MainWindow.Initialize() in MainWindow.cs:line 21

If I close Visual Studio, open it, and run my tests, they pass. If I run them again, even without doing anything else, they all fail with the above error, and continue to do so until I relaunch VS. My initialization method (currently) simply creates a new instance of the MainWindow class to be used by my tests.

If I debug the tests rather than running them, the tests pass, and then will pass once when run, just as though I'd relaunched VS.

I have a single WebBrowser control in the form. If I remove it, the above error goes away. The only code that is currently doing anything to the offending control is auto-generated by the designer.

Noticing that the call stack includes set_AllowWebBrowserDrop (which is a property I disabled in the designer), I've tried a number of different combinations of property values for properties that are unique to the WebBrowser control. I can't remember all of the combinations I've tried, but none have solved the problem, they have simply slightly changed the stack trace.

Searching for this problem hasn't yielded many useful results. Most solutions that I've found have been along the lines of changing some of the WebBrowser settings in the designer, or handling webpage navigation in a live-running program rather than a unit test.

This problem is making it very difficult to use TDD, which I'm trying to force myself into for this project so that I can get used to it for more major projects in the future.

How can I resolve this issue with the WebBrowser control so that I can successfully run my tests again?

0

There are 0 answers