#30 SetNamedParameterAction with ref parameter

v1.0 (example)
open
nobody
nmock2 (11)
5
2008-06-26
2008-06-26
No

Hello,

I've a strange problem with SetNamedParameterAction:

[Test]
public void Test1() {
Mockery mocks = new Mockery();
_cSelect mock = mocks.NewMock<_cSelect>();
int a = 0;
Array b = Array.CreateInstance(typeof(Int32), 0);
Array c = Array.CreateInstance(typeof(String), 0);
Expect.Once.On(mock).Method("GetSelected").Will(
new SetNamedParameterAction("NumberItems", a),
new SetNamedParameterAction("ObjectType", b),
new SetNamedParameterAction("ObjectName", c),
Return.Value(0));

mock.GetSelected(ref a, ref b, ref c);
mocks.VerifyAllExpectationsHaveBeenMet();
}

I get the following error:

TestCase 'Tester.ModelImporterTest.Test1'
failed: System.InvalidOperationException : cannot set the value of in parameter NumberItems
in NMock2.Monitoring.ParameterList.set_Item(Int32 i, Object value)
in NMock2.Actions.SetNamedParameterAction.Invoke(Invocation invocation)
in NMock2.Internal.BuildableExpectation.Perform(Invocation invocation)
in NMock2.Internal.UnorderedExpectations.Perform(Invocation invocation)
in NMock2.Mockery.Dispatch(Invocation invocation)
in NMock2.Internal.MockObject.Invoke(Invocation invocation)
in NMock2.Monitoring.Invoker.Invoke(Invocation invocation)
in NMock2.Monitoring.ProxiedObjectIdentity.Invoke(Invocation invocation)
in NMock2.Monitoring.ProxyInvokableAdapter.Invoke(IMessage msg)
in System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
in MyNameSpace._cSelect.GetSelected(Int32& NumberItems, Array& ObjectType, Array& ObjectName)
D:\Documents\Visual Studio 2005\Projects\PRJ 1.4\Tester\ModelImporterTest.cs(79,0): in Tester.ModelImporterTest.Test1()

From the objects viewer I see:

int GetSelected(ref int NumberItems, ref System.Array ObjectType, ref System.Array ObjectName)
Member of MyNameSpace._cSelect

The interface _cSelect is automatically obtained wrapping a COM object by Visual Studio. Tha strange thing is that the follwing code works fine:

public interface AInterface {
int method(ref Int32 a, ref Array b);
}

public class AClass {
public int method(AInterface aInterface) {
int a = 0;
Array b = null;
aInterface.method(ref a, ref b);
return a;
}
}
[Test]
public void TestRef() {
Mockery mocks = new Mockery();
AInterface mock = mocks.NewMock<AInterface>();

Stub.On(mock).Method("method").With(0, null).Will(
new SetNamedParameterAction("a", 1),
new SetNamedParameterAction("b",
new int[] { 1 }),
Return.Value(0));
AClass aClass = new AClass();

Assert.AreEqual(1, aClass.method(mock));
mocks.VerifyAllExpectationsHaveBeenMet();
}

Do you have any suggestions? I've tried Nmock version 2.0.0.59 but with no luck.

Thanks in advance,
Stenio Brunetta

Discussion

  • Thomas Weingartner

    Logged In: YES
    user_id=1140914
    Originator: NO

    Hi there

    Which COM object did you use?

    Cheers
    Thomas

     
  • Stenio Brunetta

    Stenio Brunetta - 2008-07-14

    Logged In: YES
    user_id=135731
    Originator: YES

    Hi,

    it is a COM interface to SAP2000 V12, a structural engineering program.

    A tree dump made with ildasm follows:

    | |___[INT] Sap2000._cSelect
    | | | .class interface public abstract auto ansi import
    | | | .custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 43 35 31 38 38 33 34 33 2D 36 41 44 37 // ..$C5188343-6AD7 ...
    | | | .custom instance void [mscorlib]System.Runtime.InteropServices.TypeLibTypeAttribute::.ctor(int16) = ( 01 00 D0 10 00 00 ) ...
    | | |___[MET] All : int32(bool)
    | | |___[MET] ClearSelection : int32()
    | | |___[MET] Constraint : int32(string,bool)
    | | |___[MET] CoordinateRange : int32(float64,float64,float64,float64,float64,float64,bool,string,bool,bool,bool,bool,bool,bool)
    | | |___[MET] GetSelected : int32(int32&,class [mscorlib]System.Array&,class [mscorlib]System.Array&)
    | | |___[MET] Group : int32(string,bool)
    | | |___[MET] InvertSelection : int32()
    | | |___[MET] LinesParallelToCoordAxis : int32(class [mscorlib]System.Array&,string,float64,bool)
    | | |___[MET] LinesParallelToLine : int32(string,bool)
    | | |___[MET] PlaneXY : int32(string,bool)
    | | |___[MET] PlaneXZ : int32(string,bool)
    | | |___[MET] PlaneYZ : int32(string,bool)
    | | |___[MET] PreviousSelection : int32()
    | | |___[MET] PropertyArea : int32(string,bool)
    | | |___[MET] PropertyCable : int32(string,bool)
    | | |___[MET] PropertyFrame : int32(string,bool)
    | | |___[MET] PropertyLink : int32(string,bool)
    | | |___[MET] PropertyLinkFD : int32(string,bool)
    | | |___[MET] PropertyMaterial : int32(string,bool)
    | | |___[MET] PropertySolid : int32(string,bool)
    | | |___[MET] PropertyTendon : int32(string,bool)
    | | |___[MET] SupportedPoints : int32(class [mscorlib]System.Array&,string,bool,bool,bool,bool,bool,bool,bool)

    | |___[INT] Sap2000.cSelect
    | | | .class interface public abstract auto ansi import
    | | | implements Sap2000._cSelect
    | | | .custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 43 35 31 38 38 33 34 33 2D 36 41 44 37 // ..$C5188343-6AD7 ...
    | | | .custom instance void [mscorlib]System.Runtime.InteropServices.CoClassAttribute::.ctor(class [mscorlib]System.Type) = ( 01 00 14 53 61 70 32 30 30 30 2E 63 53 65 6C 65 // ...Sap2000.cSele ...
    | |
    | |___[CLS] Sap2000.cSelectClass
    | | | .class public auto ansi import
    | | | implements Sap2000._cSelect
    | | | implements Sap2000.cSelect
    | | | .custom instance void [mscorlib]System.Runtime.InteropServices.ClassInterfaceAttribute::.ctor(int16) = ( 01 00 00 00 00 00 ) ...
    | | | .custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 35 35 38 46 44 31 42 43 2D 38 30 36 37 // ..$558FD1BC-8067 ...
    | | | .custom instance void [mscorlib]System.Runtime.InteropServices.TypeLibTypeAttribute::.ctor(int16) = ( 01 00 02 00 00 00 ) ...
    | | |___[MET] .ctor : void()
    | | |___[MET] All : int32(bool)
    | | |___[MET] ClearSelection : int32()
    | | |___[MET] Constraint : int32(string,bool)
    | | |___[MET] CoordinateRange : int32(float64,float64,float64,float64,float64,float64,bool,string,bool,bool,bool,bool,bool,bool)
    | | |___[MET] GetSelected : int32(int32&,class [mscorlib]System.Array&,class [mscorlib]System.Array&)
    | | |___[MET] Group : int32(string,bool)
    | | |___[MET] InvertSelection : int32()
    | | |___[MET] LinesParallelToCoordAxis : int32(class [mscorlib]System.Array&,string,float64,bool)
    | | |___[MET] LinesParallelToLine : int32(string,bool)
    | | |___[MET] PlaneXY : int32(string,bool)
    | | |___[MET] PlaneXZ : int32(string,bool)
    | | |___[MET] PlaneYZ : int32(string,bool)
    | | |___[MET] PreviousSelection : int32()
    | | |___[MET] PropertyArea : int32(string,bool)
    | | |___[MET] PropertyCable : int32(string,bool)
    | | |___[MET] PropertyFrame : int32(string,bool)
    | | |___[MET] PropertyLink : int32(string,bool)
    | | |___[MET] PropertyLinkFD : int32(string,bool)
    | | |___[MET] PropertyMaterial : int32(string,bool)
    | | |___[MET] PropertySolid : int32(string,bool)
    | | |___[MET] PropertyTendon : int32(string,bool)
    | | |___[MET] SupportedPoints : int32(class [mscorlib]System.Array&,string,bool,bool,bool,bool,bool,bool,bool)

    Thanks,
    Stenio

     
  • Urs Enzler

    Urs Enzler - 2008-07-15

    Logged In: YES
    user_id=1966015
    Originator: NO

    The problem is the way NMock creates its mocks (remoting proxies). It is not possible to mock ref parameters on COM interfaces. Unfortunately, you have to write your own mock class for this.

     
  • Stenio Brunetta

    Stenio Brunetta - 2008-07-17

    Logged In: YES
    user_id=135731
    Originator: YES

    Doh!

    Thanks for your help anyway.

    Regards,
    Stenio

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks