(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.
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.
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.
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 - .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With