Re: [nsubstitute] How to mock a WinForm

314 views
Skip to first unread message

David Tchepak

unread,
Apr 8, 2013, 2:38:57 AM4/8/13
to nsubs...@googlegroups.com
Hi Wilson,

Generally I wouldn't test the showForm code directly (we can assume WinForms will do the right thing, and confirm it in manual or automated UI testing). Instead we can try and test ClassA in isolation from the UI framework details. I think the most common way to do this is to use a dependency to show the additional form, and then test that is called properly from ClassA.

Approximate code:

public interface ISomeFormLauncher {
  void Show(Prop1 p1, Prop2 p2);
}

class ClassA {
  ISomeFormLauncher launcher
  public ClassA(ISomeFormLauncher launcher) { this.launcher = launcher; }

  public void ShowForm() {
    launcher.Show(SomeObject1, SomeObject2);
  }
}

[Test]
public void ShouldShowForm() {
  var launcher = Substitute.For<ISomeFormLauncher>();
  var sut = new ClassA(launcher);

  // ... setup ...
  launcher.Received().Show(expectedObj1, expectedObj2);
}

// Tested via UI (manual or automated).
public class SomeFormLauncher : ISomeFormLauncher {
    public void Show(Prop1 p1, Prop2 p2) {
        SomeForm someForm = new SomeForm();
        someForm.prop1 = p1;
        someForm.prop2 = p2;
        someForm.Show();
    }  
}

Hope this helps.

Regards,
David

On Mon, Apr 8, 2013 at 3:14 PM, Wilson Wu <stu...@gmail.com> wrote:
Hi,

I encountered a problem when trying to use NSubstitute to test a class. My class has a method to display a windows form like below

class ClassA
{
    public showForm()
    {
        //some extra things...
        // SomeForm extends Windows.Forms.Form
        SomeForm someForm = new SomeForm();
        someForm.prop1 = SomeObject1;
        someForm.prop2 = SomeObject2;
        someForm.Show();
    }
}

I think for normal case I do not need to mock a form, but inside the form I have a WebBrowser component so I always got below error:

System.Threading.ThreadStateException : ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

So I was thinking to mock the form, unfortunately I couldn't do it with NSubstitute. Even the code:

var form = Substitute.For<Form>();

gives me NullReferenceException.

Can someone please give some advice on how to test such scenario. Many thanks.

Regards,
Wilson

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at http://groups.google.com/group/nsubstitute?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Wilson Wu

unread,
Apr 8, 2013, 3:56:40 AM4/8/13
to nsubs...@googlegroups.com
Many thanks David.

You are right I should separate UI logic into separate class to make it testable.

Cheers,
Wilson
Reply all
Reply to author
Forward
0 new messages