NullReferenceException when calling the InterceptedMethod

Nov 2, 2009 at 1:04 PM

I recompiled using TypeMock 5.4.3.0 (it doesn't work otherwise) and I'm getting the following exception:

 

System.NullReferenceException: Object reference not set to an instance of an object.
at CThru.Silverlight.SilverlightUnitTestAttribute.Execute()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
d:\Didactico\SDDN\November\Tests\Trunk\WebAiiHelloWorld\TypeMockTests\TypeMockTests.cs(13,0) at TypeMockTests.TypeMock.ShouldDisplayMessage_WhenEnteringNameAndClickingButton()

 

 

Coordinator
Nov 2, 2009 at 3:11 PM
are you running with typemock enabled?
did you enable the cthru engine?

can you show some code?

On Mon, Nov 2, 2009 at 4:04 PM, mamadero <notifications@codeplex.com> wrote:

From: mamadero

I recompiled using TypeMock 5.4.3.0 (it doesn't work otherwise) and I'm getting the following exception:

 

System.NullReferenceException: Object reference not set to an instance of an object.
at CThru.Silverlight.SilverlightUnitTestAttribute.Execute()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
d:\Didactico\SDDN\November\Tests\Trunk\WebAiiHelloWorld\TypeMockTests\TypeMockTests.cs(13,0) at TypeMockTests.TypeMock.ShouldDisplayMessage_WhenEnteringNameAndClickingButton()

 

 

Read the full discussion online.

To add a post to this discussion, reply to this email (CThru@discussions.codeplex.com)

To start a new discussion for this project, email CThru@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Thanks,

Roy Osherove
www.TypeMock.com - Unit Testing, Plain Smart

Author of "The Art Of Unit Testing" (http://ArtOfUnitTesting.com )
A blog for team leaders: http://5Whys.com
my .NET blog: http://www.ISerializable.com
Twitter: http://twitter.com/RoyOsherove
+972-524-655388 (GMT+2)
Nov 2, 2009 at 10:12 PM

I'm new to Typemock and CThru, so I'll try to be really detailed to be sure I'm not missing something obvious.

This is my test class

 

[TestClass]
    public class TypeMock
    {
        [TestMethod]
        [SilverlightUnitTest]
        public void ShouldDisplayMessage_WhenEnteringNameAndClickingButton()
        {
            var page = new MainPage();
            page.nameTextBox.Text = "Miguel";
            page.Button_Click(page, null);
            page.result.Text.ShouldBe("Hello Miguel!!!");
        }

    }

I ran it again today's morning and I feel bad for not realizing before, but my nameTextBox is actually null. On my defence I was tricked yesterday while coding at 2:00 in the morning since TypeMock is able to intercept calls to null objects (really cool actually, but I wasn't expect that), so I thought it was a problem with the library.

Now the question would be. Why is it than the textbox is null? When I create the MainPage I noticed that it's loading the content in the InitializeComponent, but still the TextBox is null.

System.Windows.Application.LoadComponent(this, new System.Uri("/WebAiiHelloWorld;component/MainPage.xaml", System.UriKind.Relative));
            this.LayoutRoot = ((System.Windows.Controls.StackPanel)(this.FindName("LayoutRoot")));

Is there any other magic mocking that TypeMock is doing that breaks the LoadComponent or the FindName?

The MainPage is really simple. Just a hello world with a button, a textbox and two textblock

 

<UserControl
    x:Class="WebAiiHelloWorld.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignWidth="640"
    d:DesignHeight="480">
    <StackPanel
        x:Name="LayoutRoot"
        Orientation="Horizontal"
        Height="30"
        VerticalAlignment="Top">
        <TextBlock
            Text="Name"/>
        <TextBox 
            Width="250"
            x:Name="nameTextBox"/>
        <Button
            Content="Click me2"
            x:Name="button"
            Click="Button_Click"/>
        <TextBlock 
            x:Name="result"/>
    </StackPanel>
</UserControl>

 

 

 

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        internal void Button_Click(object sender, RoutedEventArgs e)
        {
            result.Text = string.Format("Hello {0}!!!", nameTextBox.Text);
        }
    }
Coordinator
Nov 3, 2009 at 6:43 AM
loook at this video:
about the last quarter or third of the video shows how I write unit tests with silverunit

your textbox is null because silverunit does not load the wpf code. that would have made it an integration test.
instead, you instantiate the internal objects directly and then call SilverUnit.SetTemplateChild(..) to inject your control instances.
then call OnApplyTemplate menually to have whatevr things you want to happen in that context, happen.



On Tue, Nov 3, 2009 at 1:12 AM, mamadero <notifications@codeplex.com> wrote:

From: mamadero

I'm new to Typemock and CThru, so I'll try to be really detailed to be sure I'm not missing something obvious.

This is my test class

 

[TestClass]
    public class TypeMock
    {
        [TestMethod]
        [SilverlightUnitTest]
        public void ShouldDisplayMessage_WhenEnteringNameAndClickingButton()
        {
            var page = new MainPage();
            page.nameTextBox.Text = "Miguel";
            page.Button_Click(page, null);
            page.result.Text.ShouldBe("Hello Miguel!!!");
        }

    }

I ran it again today's morning and I feel bad for not realizing before, but my nameTextBox is actually null. On my defence I was tricked yesterday while coding at 2:00 in the morning since TypeMock is able to intercept calls to null objects (really cool actually, but I wasn't expect that), so I thought it was a problem with the library.

Now the question would be. Why is it than the textbox is null? When I create the MainPage I noticed that it's loading the content in the InitializeComponent, but still the TextBox is null.

System.Windows.Application.LoadComponent(this, new System.Uri("/WebAiiHelloWorld;component/MainPage.xaml", System.UriKind.Relative));
            this.LayoutRoot = ((System.Windows.Controls.StackPanel)(this.FindName("LayoutRoot")));

Is there any other magic mocking that TypeMock is doing that breaks the LoadComponent or the FindName?

The MainPage is really simple. Just a hello world with a button, a textbox and two textblock

 

<UserControl
    x:Class="WebAiiHelloWorld.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignWidth="640"
    d:DesignHeight="480">
    <StackPanel
        x:Name="LayoutRoot"
        Orientation="Horizontal"
        Height="30"
        VerticalAlignment="Top">
        <TextBlock
            Text="Name"/>
        <TextBox 
            Width="250"
            x:Name="nameTextBox"/>
        <Button
            Content="Click me2"
            x:Name="button"
            Click="Button_Click"/>
        <TextBlock 
            x:Name="result"/>
    </StackPanel>
</UserControl>

 

 

 

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        internal void Button_Click(object sender, RoutedEventArgs e)
        {
            result.Text = string.Format("Hello {0}!!!", nameTextBox.Text);
        }
    }

Read the full discussion online.

To add a post to this discussion, reply to this email (CThru@discussions.codeplex.com)

To start a new discussion for this project, email CThru@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Thanks,

Roy Osherove
www.TypeMock.com - Unit Testing, Plain Smart

Author of "The Art Of Unit Testing" (http://ArtOfUnitTesting.com )
A blog for team leaders: http://5Whys.com
my .NET blog: http://www.ISerializable.com
Twitter: http://twitter.com/RoyOsherove
+972-524-655388 (GMT+2)
Coordinator
Nov 3, 2009 at 6:46 AM
BTW, silverunit was primarily design for testing silverlight custom controls.
it might need some adaptation to make it also work with pages and user controls, where InitializeComponent does all the work.

On Tue, Nov 3, 2009 at 1:12 AM, mamadero <notifications@codeplex.com> wrote:

From: mamadero

I'm new to Typemock and CThru, so I'll try to be really detailed to be sure I'm not missing something obvious.

This is my test class

 

[TestClass]
    public class TypeMock
    {
        [TestMethod]
        [SilverlightUnitTest]
        public void ShouldDisplayMessage_WhenEnteringNameAndClickingButton()
        {
            var page = new MainPage();
            page.nameTextBox.Text = "Miguel";
            page.Button_Click(page, null);
            page.result.Text.ShouldBe("Hello Miguel!!!");
        }

    }

I ran it again today's morning and I feel bad for not realizing before, but my nameTextBox is actually null. On my defence I was tricked yesterday while coding at 2:00 in the morning since TypeMock is able to intercept calls to null objects (really cool actually, but I wasn't expect that), so I thought it was a problem with the library.

Now the question would be. Why is it than the textbox is null? When I create the MainPage I noticed that it's loading the content in the InitializeComponent, but still the TextBox is null.

System.Windows.Application.LoadComponent(this, new System.Uri("/WebAiiHelloWorld;component/MainPage.xaml", System.UriKind.Relative));
            this.LayoutRoot = ((System.Windows.Controls.StackPanel)(this.FindName("LayoutRoot")));

Is there any other magic mocking that TypeMock is doing that breaks the LoadComponent or the FindName?

The MainPage is really simple. Just a hello world with a button, a textbox and two textblock

 

<UserControl
    x:Class="WebAiiHelloWorld.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignWidth="640"
    d:DesignHeight="480">
    <StackPanel
        x:Name="LayoutRoot"
        Orientation="Horizontal"
        Height="30"
        VerticalAlignment="Top">
        <TextBlock
            Text="Name"/>
        <TextBox 
            Width="250"
            x:Name="nameTextBox"/>
        <Button
            Content="Click me2"
            x:Name="button"
            Click="Button_Click"/>
        <TextBlock 
            x:Name="result"/>
    </StackPanel>
</UserControl>

 

 

 

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        internal void Button_Click(object sender, RoutedEventArgs e)
        {
            result.Text = string.Format("Hello {0}!!!", nameTextBox.Text);
        }
    }

Read the full discussion online.

To add a post to this discussion, reply to this email (CThru@discussions.codeplex.com)

To start a new discussion for this project, email CThru@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Thanks,

Roy Osherove
www.TypeMock.com - Unit Testing, Plain Smart

Author of "The Art Of Unit Testing" (http://ArtOfUnitTesting.com )
A blog for team leaders: http://5Whys.com
my .NET blog: http://www.ISerializable.com
Twitter: http://twitter.com/RoyOsherove
+972-524-655388 (GMT+2)
Nov 3, 2009 at 12:07 PM

Roy,

Thanks for the link. I'll watch the video tomorrow and continue working with it. It made sense once I understood what was really happening. I agree, it would be useful to support this scenario and I understand what was the primary design goal.

 

Thanks

Miguel