Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set a property with no setter using Moq?

Tags:

c#

moq

Im doing some research. I want to use moq and pass it to an MVC controller so allow it to set some values in session. I have written some code to see if its possible to 'open up' a property with no setter. Just don't know if its possible...

The following code was my attempt at trying to set a property witn no setter!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Moq;

namespace TestMoq
{
    class Program
    {
        static void Main(string[] args)
        {
            var mock = new Mock<TestClass>();

            mock.SetupProperty(f => f.VarWithNoSetter);

            mock.Object.VarWithNoSetter = "Set";

            Console.WriteLine(mock.Object.VarWithNoSetter);

            Console.ReadLine();
        }
    }

    public class TestClass
    {
        private string _varWithNoSetter;
        public string VarWithNoSetter
        {
            get { return _varWithNoSetter; }
        }

        public TestClass()
        {
        }
    }
}
like image 732
Exitos Avatar asked Mar 19 '12 16:03

Exitos


People also ask

How do you set a mock object property?

You can set the mock object property to the value returned by the mock object method. To achieve this, specify separate behaviors for the method and the property. You can specify one behavior at a time. For more information about mock object behavior, see Specify Mock Object Behavior.

What can be mocked with MOQ?

You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces.

How do you mock a method in MOQ?

First, we instantiate the FakeDbArticleMock class and indicate which setup we want to use for this test. Then, it is necessary to instantiate the repository we want to test and inject the mock instance into it. Finally, we call the method we are testing and assert the results.

What does MOQ setup do?

'Setup' mocks a method and 'Returns' specify what the mocked method should return. 'Verifiable' marks this expectation to verified at the end when Verify or VerifyAll is called i.e. whether AddIncomePeriod was called with an object of IncomePeriod and if it returned the same output.


3 Answers

Sure:

mock.SetupGet(f => f.VarWithNoSetter).Returns("Hi, Exitos!");
like image 69
Craig Stuntz Avatar answered Oct 05 '22 01:10

Craig Stuntz


For me it doesn't work with SetUpGet, without making the property virtual. However it works by using Mock.Of<>:

var mock = Mock.Of<TestClass>(m=>m.VarWithNoSetter == "the value");
like image 43
dan zetea Avatar answered Oct 05 '22 01:10

dan zetea


Yes you can but you have to make that property virtual at it will be possible as Moq will generate a proxy

public virtual string VarWithNoSetter
{
   get { return _varWithNoSetter; }
}

or you can use .SetUpGet

like image 26
Massimiliano Peluso Avatar answered Oct 05 '22 02:10

Massimiliano Peluso