0

I basically want to stub a function, but define my own equality comparer for a Reference Type parameter.

I want to stub a function to return data. I want the parameter of the method to compare by a particular value instead of a ReferenceEquals. I also do not want to create an equals override for my parameter reference type. I think below is the way to accomplish this but I am receiving an exception. Is there another way to do this and/or what I am doing wrong here?

Exception message: System.Reflection.AmbiguousMatchException : Ambiguous match found.

    public class Parameter
    {
        public string Property1 { get; set; }
    }

    public interface IStubbable
    {
         string DoStuff(Parameter param);
    }

    public class ThisService
    {
        private IStubbable _stubbable;
        public ThisService(IStubbable stubbable)
        {
            _stubbable = stubbable;
        }

        public string DoTheStuff(Parameter param)
        {
            return _stubbable.DoStuff(param);
        }
    }

    [Test]
    public void TestStubbing()
    {
        const string expectedResult = "Totes";

        var iStub = MockRepository.GenerateStub<IStubbable>();

        const string prop1 = "cool stub bro";
        iStub
            .Stub(x => x.DoStuff(Arg<Parameter>.Matches(y => y.Property1 == prop1)))
            .Return(expectedResult);


        var service = new ThisService(iStub);

        var result = service.DoTheStuff(new Parameter() {Property1 = prop1});

        Assert.AreEqual(expectedResult, result);
    }
Cirem
  • 840
  • 1
  • 11
  • 15

1 Answers1

0

This post provided another way of achieving this. Rhino Mocks - Using Arg.Matches

Change the stub to:

        iStub.Stub(x => x.DoStuff(new Parameter()))
            .Constraints(new PredicateConstraint<Parameter>(y => y.Property1 == prop1 ))
            .Return(expectedResult);
Community
  • 1
  • 1
Cirem
  • 840
  • 1
  • 11
  • 15