views:

341

answers:

3

I need to use some WPF components in an NUnit unit test. I run the test through ReSharper, and it fails with the following error when using the WPF object:

System.InvalidOperationException:

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

I've read about this problem, and it sounds like the thread needs to be STA, but I haven't figured out how to do this yet. What triggers the problem is the following code:

[Test]
public void MyTest()
{
    var textBox = new TextBox(); 
    textBox.Text = "Some text"; // <-- This causes the exception.
}
+2  A: 

Have you tried this?

HTH,
Kent

Kent Boogaart
Thx! Just found the same solution myself, and it works. Apparently ReSharper uses an older version of nUnit, and therefore I can't simply use the [RequiresSTA] attribute.
stiank81
A: 

There's so much you have to do to get this to work.

This tool might help.

Ray
It really doesn't take much to make this work - once you know what to do. See Kents answer or this link: http://www.hedgate.net/articles/2007/01/08/instantiating-a-wpf-control-from-an-nunit-test/ Thanks for the link to the tool though.
stiank81