I see three aspects here:
When a method with a reference type return value is called on a mock object and no behavior has been set up for the call, the mock object will return a mock. If you want it to return
null
instead, you have to configure that explicitly. Thus, it is not enough to set uphost.WhenToldTo(h => h.GetTenantInstance(Param.Is(new Uri("http://foo.bar")))) .Return(new TenantInstance());
You also have to set up the other case with something like this:
host.WhenToldTo(h => h.GetTenantInstance(Param<Uri>.Matches(x => !x.Equals(new Uri("http://foo.bar"))))) .Return((TenantInstance)null);
I find your "workaround" solution more elegant than these two setups.
When you match a method call argument for equality, there is no need to use
Param.Is()
. You can simply set up the behavior withhost.WhenToldTo(h => h.GetTenantInstance(new Uri("http://foo.bar"))) .Return(new TenantInstance());
- The fact that you get an exception when using
Param.Is()
here is a shortcoming of Machine.Fakes. I see not reason why this should not work. I will correct that at some point and let you know.