简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM