Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 912
ahsteele Avatar asked Jul 09 '12 22:07

ahsteele


1 Answers

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);
like image 195
Stefan Steinegger Avatar answered Sep 28 '22 04:09

Stefan Steinegger