Search code examples
c#mockingrhino-mocksstubrhino-mocks-3.5

With Rhino Mocks how to stub a method that makes use of the params keyword?


I am attempting to setup an expectation on a repository. The method makes use of the params keyword:

string GetById(int key, params string[] args);

The expectation I have setup:

var resourceRepo = MockRepository.GenerateMock<IResourceRepository>();
resourceRepo.Expect(r => r.GetById(
    Arg<int>.Is.Equal(123),
    Arg<string>.Is.Equal("Name"),
    Arg<string>.Is.Equal("Super"),
    Arg<string>.Is.Equal("Mario"),
    Arg<string>.Is.Equal("No"),
    Arg<string>.Is.Equal("Yes"),
    Arg<string>.Is.Equal("Maybe")))
    .Return(String.Empty);

throws this exception:

Test method XYZ threw exception: System.InvalidOperationException: Use Arg ONLY within a mock method call while recording. 2 arguments expected, 7 have been defined.

What is wrong with the setup of my expectation?


Solution

  • params is just an array:

    var resourceRepo = MockRepository.GenerateMock<IResourceRepository>();
    resourceRepo
      .Expect(r => r.GetById(
        Arg<int>.Is.Equal(123),
        Arg<string[]>.List.ContainsAll(new[]
                                       {
                                           "Name",
                                           "Super",
                                           "Mario",
                                           "No",
                                           "Yes",
                                           "Maybe"
                                       })))
      .Return(String.Empty);