简体   繁体   English

从NUnit的Assert抛出的RuntimeBinderException。

[英]RuntimeBinderException thrown from NUnit's Assert.That for dynamic objects

I have an NUnit test case that asserts the type set in the new ViewBag property of an MVC Controller. 我有一个NUnit测试用例,它声明了MVC控制器的新ViewBag属性中设置的类型。

So the action body has 所以动作主体有

 using (IRepository repository = _repositoryProvider.GetRepository())
 {
      ViewBag.Articles = repository.Get<Articles>()
      return View();
 }

and trying to test this as so 并尝试如此测试

 var mockProvider = new Mock<IRepositoryProvider>();
 var mockRepository = new Mock<IRepository>();
 mockProvider.Setup(m => m.GetRepository()).Returns(mockRepository.Object);
 mockRepository.Setup(m => m.Get<Articles>()).Returns(It.IsAny<IEnumerable<Articles>>);
 var homeController = new HomeController(mockProvider.Object);
 var viewResult = homeController.Index();
 Assert.That(homeController.ViewBag.Articles, Is.TypeOf<IEnumerable<Articles>>());

Now, the "That" call throws a RuntimeBinderException 现在,“ That”调用将引发RuntimeBinderException

 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 
 The call is ambiguous between the following methods or properties:  

 NUnit.Framework.Assert.That(NUnit.Framework.Constraints.ActualValueDelegate, 
 NUnit.Framework.Constraints.IResolveConstraint) and 

 NUnit.Framework.Assert.That(NUnit.Framework.TestDelegate, 
 NUnit.Framework.Constraints.IResolveConstraint)

Has anyone seen an exception for custom dynamic objects? 有没有人看到自定义动态对象的异常? I have other test cases where strings are set in the ViewBag and they don't run into this exception 我还有其他测试用例,其中在ViewBag中设置了字符串,并且它们没有遇到此异常

I also tried "as dynamic" as in, but that didn't help either 我也尝试过“像动态一样”,但这也无济于事

ViewBag.Articles = repository.Get<Articles>() as dynamic;

This is a super old question so you've probably found your answer by now, but since this was the first google result for my problem (and this page didn't have an answer on it) I figured it'd be good to include a solution. 这是一个非常古老的问题,所以您现在可能已经找到了答案,但是由于这是我遇到的问题的第一个Google搜索结果(而且此页面没有答案),所以我认为最好包括一个解法。

I believe what was happening is that as pointed out by the other poster, there was a problem with your mocks causing homeController.ViewBag.Articles to be null. 我相信发生的事情是,正如其他海报指出的那样,您的homeController.ViewBag.Articles存在问题,导致homeController.ViewBag.Articles为null。 nUnit has a hard time with nulls on a dynamic object which was causing the RuntimeBinderException. nUnit很难在动态对象上使用null,这会导致RuntimeBinderException。

In your case fixing your mock would solve the issue, but in the general case (ie someone who actually wants to use nUnit with a null on a dynamic object), the fix is to cast the dynamic value to the concrete type you want to check against and it will solve the ambiguous call: 在您的情况下,修复模拟可以解决问题,但是在一般情况下(即,实际上要在动态对象上使用nUnit的nUnit的人),解决方法是将动态值转换为要检查的具体类型对,它将解决歧义调用:

Assert.That((IEnumerable<Articles>)homeController.ViewBag.Articles, Is.TypeOf<IEnumerable<Articles>>());

You have error in setup part of Your unit test 您在单元测试的设置部分出错

I don't know how to enter angle bracket in code below so I will use [ 我不知道如何在下面的代码中输入尖括号,因此我将使用[

mockRepository.Setup(m => m.Get[Articles]()).Returns(It.IsAny[IEnumerable[Articles]];

'Returns' method should receive object that method should return. “返回”方法应接收该方法应返回的对象。 It.IsAny is used to specify input parameters to the method. It.IsAny用于指定方法的输入参数。 You have to use it like that. 您必须像那样使用它。


var mock = new Mock[ITestInterface]();
mock.Setup(m => m.GetListOfMyClass(It.IsAny[int]())).Returns(new List[MyClass]());
var result = mock.Object.GetListOfMyClass(10);
Assert.That(result, Is.TypeOf[List[MyClass]]());

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

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