Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7443729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:22:43+00:00 2026-05-29T11:22:43+00:00

I’m trying to write a unit test for a sample project that uses the

  • 0

I’m trying to write a unit test for a sample project that uses the MVP architecture.
When I run the test, I have the following error message:

UnitTests.Tests.testCopy:
Expected: “Testing …”
But was: null

I post below the snippets I did:

namespace Copy
{
    public interface IView
    {
        string Copy { get; set; }
        string Original { get; }
        event EventHandler Changed;
    }

    public class Presenter
    {
        private IView view;
        public Presenter(IView view)
        {
            this.view = view;
            this.view.Changed += new EventHandler(OnChanged);
        }

        public void OnChanged(object sender, EventArgs e)
        {
            view.Copy = view.Original;
        }
    }

    public partial class Form1 : Form, IView
    {
        private Presenter presenter;

        public Form1()
        {
            InitializeComponent();
            presenter = new Presenter(thisIView);
            this.t_originalviewMock.TextChanged += OnChanged;
        }

        public string Original
        {
            get { return t_originalMockInstance).Text; }
        }

        public string Copy
        {
            get { return t_copy.Text; }
            set { t_copy.Text = value;}
        }

        private void OnChanged(object sender, EventArgs e)
        {
            if (Changed != null)
                Changed(sender, e);
        }

        public event EventHandler Changed;
    }
}

Unit test code:

namespace UnitTests
{
    [TestFixture]
    public class Tests
    {
        private DynamicMock viewMock;
        private Presenter presenter;

        [SetUp]
        public void setup()
        {
            viewMock = new DynamicMock(typeof(IView));
            presenter = new Presenter((IView)viewMock.MockInstance);
        }

        [Test]
        public void testCopy()
        {
            viewMock.ExpectAndReturn("get_Original", "Testing ...");
            viewMock.Expect("get_Cpoy");
            Assert.AreEqual("Testing ...", 
                ((IView)viewMock.MockInstance).Copy);
        }
    }
}

Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:22:44+00:00Added an answer on May 29, 2026 at 11:22 am

    “I’m trying to test that a change in Original textBox, leads to a change in Copy textBox.”

    Here’s how you should set up your test then:

    1. Tell view mock to raise Changed event
    2. Tell view mock to return "Testing ..." upon call to get_Original property getter
    3. Tell view mock you’re expecting call to Copy setter with "Testing ..." value
    4. Verify mock expectations

    I’m not sure whether raising events is possible with NUnit.Mocks. Judging by fairly tiny API, I’d say no. Yet you can go around this problem by invoking event handler directly (since it’s public anyways):

    viewMock.ExpectAndReturn("get_Original", "Testing ...");
    viewMock.Expect("set_Copy", "Testing ...");
    
    presenter.OnChanged(viewMock, EventArgs.Empty);
    
    // here we verify expectations set on mock object; 
    // that Copy setter was called with "Testing ..." value
    viewMock.Verify();
    

    You can see that this test indeed works by simply changing set_Copy expected value to something else than "Testing ..." – it will fail with appropriate message.

    Mocking frameworks sidenote

    Any reason you’re using NUnit.Mocks instead of more serious mocking framework? Give FakeItEasy, Moq or RhinoMocks a look.

    This is how your test could look like if you’d used FakeItEasy instead:

    [Test]
    public void WhenViewChangedEventIsRaised_PresenterSetsCopyProperty()
    {
        var viewMock = A.Fake<IView>();
        var presenter = new Presenter(viewMock);
        A.CallTo(() => viewMock.Original).Returns("Testing ...");
    
        viewMock.Changed += Raise.With(EventArgs.Empty).Now;
    
        Assert.AreEqual(viewMock.Copy, "Testing ...");
    }
    

    Note that problems like typing Cpoy instead of Copy immediately go away. Not to mention you won’t have to make your event handlers exposed in public API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.