Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing WPF UserControls

Is it possible to test my WPF UserControls from NUnit (or similar)? If I create an instance of the usercontrol in a unit test like so:

// Create an instance of the WPF UserControl
var view = new ChildrenListView();

I get the following error:

"The calling thread must be STA, because many UI components require this"

I get the feeling I'm missing something very important here.

like image 884
Steve Avatar asked Oct 14 '25 06:10

Steve


1 Answers

Use the [STAThread] attribute.

    [Test]
    [STAThread]
    public void TestConstructorDoesNotThrow()
    {
        Expect(() => new ChildrenListView(), Throws.Nothing);
    }
like image 197
RJFalconer Avatar answered Oct 18 '25 05:10

RJFalconer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!