简体   繁体   English

使用Rhino Mocks如何存根使用params关键字的方法?

[英]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 : 该方法使用params关键字

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. 测试方法XYZ抛出异常:System.InvalidOperationException:在记录时在模拟方法调用中仅使用Arg。 2 arguments expected, 7 have been defined. 预计有2个参数,已经定义了7个。

What is wrong with the setup of my expectation? 设定我的期望有什么问题?

params is just an array: params只是一个数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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