How to mock a property using NUnit?

16,378

To mock the Names property in the following example...

Interface IView {    
    List<string> Names {get; set;} 
}

public class Presenter {    
    public List<string> GetNames(IView view)    {
       return view.Names;    
    } 
}

NUnit Mock Solution for a Property

using NUnit.Mocks;

In NUnit a PropertyName can be mocked with get_PropertyName to mock the get accessor and set_PropertyName to mock the set accessor, using the mock library's Expect*(..) methods like so:

List names = new List {"Test", "Test1"};
DynamicMock mockView = new DynamicMock(typeof(IView));

mockView.ExpectAndReturn("get_Names", names);

IView view = (IView)mockView.MockInstance;
Assert.AreEqual(names, presenter.GetNames(view));

Therefore, in our specific code sample at the top, the .Names property is mocked as either get_Names or set_Names.


Etc.

This blog post provides additional insight considering NUnit seemingly provides mock methods to only target methods:

I started thinking about it and realized that Property getters and setters are just treated as speciallly named methods under the covers

Share:
16,378
John K
Author by

John K

Defining quotes: "The present letter is a very long one, simply because I had no leisure to make it shorter." — Blaise Pascal "The early bird gets the worm, but the second mouse gets the cheese." —Steven Wright "And yet what are we to do about this terribly significant business of other people." — Philip Roth

Updated on June 14, 2022

Comments

  • John K
    John K almost 2 years

    How do I mock a property using NUnit?


    NOTE: I found this peripheral mocking answer to be extremely useful and repurposed it as a distinct question and answer entry here for others to find.

    Other answers welcome too.

    NUnit-Discuss Note: NUnit Mocks was created over a weekend as a toy mock implementation [...] I'm beginning to think that was a mistake, because you are far from the first person to become reliant on it.
    -- http://groups.google.com/group/nunit-discuss/msg/55f5e59094e536dc
    (Charlie Pool on NUnit Mocks)

  • AlwaysLearning
    AlwaysLearning over 12 years
    I've fleshed out John K's example so it actually compiles under .NET 2.0 - note that wherever you use mock.Expect() or mock.ExpectAndReturn() you should always follow it up with mock.Verify()...
  • AlwaysLearning
    AlwaysLearning over 12 years
    Stupid short-text editor... using System.Collections.Generic; using NUnit.Framework; using NUnit.Mocks; namespace StackExchange5331412 { public interface IView { List<string> Names { get; set; } } public class Presenter { public List<string> GetNames(IView view) { return view.Names; } }
  • AlwaysLearning
    AlwaysLearning over 12 years
    ` [TestFixture] public class TestIView { [Test] public void TestNames() { List<string> names = new List<string>(); names.AddRange(new string[] { "Test", "Test1" }); DynamicMock mockView = new DynamicMock(typeof(IView)); mockView.ExpectAndReturn("get_Names", names); //mockView.Expect("set_Names", names); IView view = (IView)mockView.MockInstance; Presenter presenter = new Presenter(); Assert.AreEqual(names, presenter.GetNames(view)); mockView.Verify(); } } }`
  • Pang
    Pang about 4 years
    Link in answer is giving "This site can’t be reached".