Alright, let’s assume I have a class like the following …
public class Foo : IFoo
{
public string Bar
{
get { ... }
}
public void Initialize()
{
...
}
}
… and as you can see it implements an interface so I can mock it. Now, in my unit test I’m building the mock like this …
var mock = new Mock<IFoo>();
mock.SetupProperty(p => p.Bar).SetReturnsDefault("Some static value here.");
… however, when the test runs I get the following error …
System.ArgumentException: Property IFoo.Bar is read-only.
Parameter name: expression
So, three questions:
- What am I doing wrong?
- What do I need to do?
- Can you please explain how I misunderstood
SetReturnsDefault?
Thanks all!
Obviously, the error message is telling you that you can’t mock the read-only property like that. Instead, try:
If you want ALL string properties which are not explicitly set up to return some string then do this: