Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Mock a readonly property whose value depends on another property of the Mock

Tags:

(As indicated by the tags, I am using moq).

I have an interface like this:

interface ISource {   string Name { get; set; }   int Id { get; set; } }  interface IExample {   string Name { get; }   ISource Source { get; set; } } 

In my application, concrete instances of IExample accept a DTO (IDataTransferObject) as the Source. Some properties on the concrete implementation of IExample are simply delegated to the Source. Like this...

class Example : IExample {   IDataTransferObject Source { get; set; }    string Name { get { return _data.Name; } } } 

I would like to create a standalone mock of IExample (standalone meaning that I cannot use a captured variable because several instances of the IExample mock will be created in the course of a test) and setup the Mock such that IExample.Name returns the value of IExample.Source.Name. So, I would like to create a mock something like this:

var example = new Mock<IExample>(); example.SetupProperty(ex => ex.Source); example.SetupGet(ex => ex.Name).Returns(what can I put here to return ex.Source.Name); 

Essentially, I want to configure the mock to return, as the value of one property, the value of a property of a subobject of the mock.

Thanks.

like image 266
wageoghe Avatar asked Jul 18 '12 16:07

wageoghe


People also ask

Can I assign value to readonly property C#?

A readonly field can't be assigned after the constructor exits. This rule has different implications for value types and reference types: Because value types directly contain their data, a field that is a readonly value type is immutable.

How do I set the value of a readonly property in VB net?

Assigning a Value. Code consuming a ReadOnly property cannot set its value. But code that has access to the underlying storage can assign or change the value at any time. You can assign a value to a ReadOnly variable only in its declaration or in the constructor of a class or structure in which it is defined.

How do I change the readonly property?

You can use mapping modifiers to change a readonly property to mutable in TypeScript, e.g. -readonly [Key in keyof Type]: Type[Key] . You can remove the readonly modifier by prefixing the readonly keyword with a minus - .

What is read only property in C sharp?

Read only means that we can access the value of a property but we can't assign a value to it. When a property does not have a set accessor then it is a read only property. For example in the person class we have a Gender property that has only a get accessor and doesn't have a set accessor.


1 Answers

You could probably use:

example.SetupGet(ex => ex.Name).Returns(() => example.Object.Source.Name); 

The value to be returned will then be determined when the property is accessed, and will be taken from Name property of the mock's Source property.

like image 136
Iridium Avatar answered Nov 16 '22 19:11

Iridium