English 中文(简体)
在 Mvc3 中的 Moq 软件包失败
原标题:Assert.IsInstanceOfType failed in Moq Package in Mvc3

I am Using Moq package for testing a Controller. HomeController.cs

public class HomeController : Controller
{  readonly IPermitRepository _repository;
    public HomeController(IPermitRepository rep)
    {            this._repository = rep;        }

    public ViewResult Index()
    {
        ViewBag.Message = "Hello World";
        PermitModel model = _repository.GetPermitDetails();
        return View(model);
    }
}

家庭主控测试中 ccs

 [TestClass]
 Public class HomeControllerTest
{
    [TestMethod]
    public void Index()
    {       
        var messagingService = new Mock<IPermitRepository>();
        var controller = new HomeController(messagingService.Object);

        var result = controller.Index() as ViewResult;
       Assert.IsInstanceOfType(result.Model, typeof(PermitModel));
    }
}

But its giving error. Assert.IsInstanceOfType failed. Expected type:. Actual type:<(null)>.

Can some one provide solution and also some inf about Moq package in MVC3. Thanks in advance

最佳回答

Moq 返回每个非无效方法调用时默认返回 null

所以当您在控制器中调用 reposititory. GetPermitDetails (); 它返回 null ,这就是测试失败的原因。

您需要调用 < code> setup 的方法返回某种东西 :

var messagingService = new Mock<IPermitRepository>();
messagingService.Setup(m => m.GetPermitDetails()).Returns(new PermitModel());
var controller = new HomeController(messagingService.Object);

您可以在 < a href=> http://code.google.com/p/moq/wiki/QuickStart" rel = "nofol" 中找到更多关于如何定制模拟行为的信息。 > Moq 快速启动

问题回答

暂无回答




相关问题
Moq Linq-to-SQL readonly property

I have a table aspnet_User in my model(dbml file) where I have a property UserName which is ReadOnly. I thought I could do this. var mockAsp_NetUser = new Mock<aspnet_User>(); mockAsp_NetUser....

Moq Roles.AddUserToRole test

I am writing unit tests for a project in ASP.NET MVC 1.0 using Moq and MvcContrib TestHelper classes. I have run into a problem. When I come to Roles.AddUserToRole in my AccountController, I get a ...

How to test method call order with Moq

At the moment I have: [Test] public void DrawDrawsAllScreensInTheReverseOrderOfTheStack() { // Arrange. var screenMockOne = new Mock<IScreen>(); var ...

Moq and DataContext

I am new to Moq and need to know if I am doing this right. In AccountController.cs I have this: int id = _userRepository.GetProfileFromUserName(userName).ProfileID; UserRepository is mocked, ...

热门标签