|
From: Mike B. <mbr...@vi...> - 2003-11-14 02:09:21
|
Does NMock not work with properties (yet)? I'm new to NMock (and C#) and am
having trouble with the following code where I try to set a property to an
expected value. When I access the property I get a null. I had been
assuming this was my own fault until I read the attached message in the
mailing list archive.
public interface IRegistry {
// This property is causing me troubles.
IRegistryKey LocalMachine { get; }
}
public interface IRegistryKey {
IRegistryKey OpenSubKey( string name);
string[] GetSubKeyNames();
object GetValue( string name);
}
public class RegistryFacade {
private IRegistry registry;
public RegistryFacade( IRegistry registry) {
this.registry = registry;
}
public InstalledJDK[] getInstalledJDKs() {
// This throws an null reference exception, because LocalMachine
is null.
IRegistryKey jdkKey = registry.LocalMachine.OpenSubKey("foo");
}
}
[NUnit.Framework.TestFixture]
public class TestRegistryFacade {
[NUnit.Framework.Test]
public void test02() {
NMock.IMock registry = new NMock.DynamicMock( typeof( IRegistry));
NMock.IMock localMachine = new NMock.DynamicMock( typeof(
IRegistryKey));
registry.ExpectAndReturn("get_LocalMachine",
(IRegistryKey)localMachine.MockInstance);
RegistryFacade facade = new RegistryFacade(
(IRegistry)registry.MockInstance);
InstalledJDK[] installedJdks = facade.getInstalledJDKs();
}
}
Mike
-----
I had a look at this today. SetupResult() does work, but as soon as you
Expect() on the same property's setter, the mock's internal record of the
property's expected signature is overwrittten (ie it expects the getter to
be called with an argument).
As I'm not that familiar with the internals of Method, Mock and co. I
thought I would ask you lot for input before I fixed it. My feeling is
that properties should be treated differently to methods (not necessarily
at the public API level, but internally). Although properties are
technically methods, they have, conceptually, two signatures. At the
moment, NMock doesn't care whether it's invoking a getter or a setter (it
even strips the "get_" and "set_" from the names), and that's essentially
the problem - calls to "get_Foo" and "set_Foo" are just recorded as a call
to "Foo".
So unless anyone has a better idea, I'll extend ClassGenerator and Mock to
treat properties differently.
Jim
Thoughtworks
|