locked
What happens when you try to use a Stub on a "Normal" class?

    Question

  • So I made a program to test stubs, because I am not sure what their limitations are.  The main page for Stubs says that "Stubs may be used on interfaces, abstract classes or non-sealed classes. ".  "Hmmmm" I thought when I read this.   "Most of my classes are "non-sealed" classes.  Does that mean I can use them on a "normal" class that does not have an interface?

    So I took an example project (a simple address book) and added the moles to the assembly then I tried to create a stub on the un interfaced Form class (that looks like this:

     

    public partial class AddAddressForm : Form
    {
         public AddAddressForm()
         {
             InitializeComponent();
         }
    
         public AddAddressForm(AddressItem item)
         {
            firstNameTextBox.Text = item.FirstName;
            lastNameTextBox.Text = item.LastName;
            address1TextBox.Text = item.Address1;
            address2TextBox.Text = item.Address2;
            cityTextBox.Text = item.City;
            stateComboBox.Text = item.State;
            zipCodeTextBox.Text = item.ZipCode;
        }
    ....

    Test:

    [TestMethod]
    public void TestInitializeWithItem()
    {
       var addressBook = new SAddAddressForm(CreateAddressBob());
    .........

    But then when I called addressBook.ShowDialog() a dialog box showed (so clearly some of the base class is being called) (note that I do not have CallBase = true here).

    Also, I tried to override some methods and properties and they were not in the intellisense and would not comple. So here are my guesses/questions:

    • If you stub an actual class, some things will work.  But what? that is question #1.  Show Dialog worked, but the call to "firstNameTextBox.Text = item.FirstName; " in the constructor failed (because the dialog did not have the text box firstNameTextBox on it).  (I had to use the debugger to get around those calls).  So, my custom textboxes were not there, but the code to make the form show with a title bar and such was...... Hmmmm
    • Only Virtual Methods can be overwritten.  This is a guess, but since virtual methods work more like interfaces, I am guessing that they will work, where a normal method would need to use moles to get it overwritten.
    Please let me know what the limits of Stubs are when then are not used with an interface or an abstract class (just a normal Non-Sealed class).

     

    Monday, September 13, 2010 6:24 PM

Answers

  • As you hinted, Stubs only works for virtual methods - including interfaces. Stubs are actually very simple - they inherit from the base type and override or implement all virtual methods.


    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    • Marked as answer by Vaccanoll Monday, September 13, 2010 7:34 PM
    Monday, September 13, 2010 7:32 PM

All replies

  • As you hinted, Stubs only works for virtual methods - including interfaces. Stubs are actually very simple - they inherit from the base type and override or implement all virtual methods.


    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    • Marked as answer by Vaccanoll Monday, September 13, 2010 7:34 PM
    Monday, September 13, 2010 7:32 PM
  • Thanks for the confirmation.  It may not be a bad idea to clarify this on the Stubs page .
    Monday, September 13, 2010 7:35 PM