简体   繁体   English

如何使用Mock测试我的控制器?

[英]How can I use Mock to test my controller?

I have a controller: 我有一个控制器:

public class SelectController : Controller {
    private readonly IChartService _chartService;
    private readonly IProductService _productService;
    private readonly IStoreService _storeService;

    public SelectController ( IChartService chartService,
                              IProductService productService,
                              IStoreService storeService ) {
        _chartService = chartService;
        _productService = productService;
        _storeService = storeService;
    }

    [HttpGet]
    [Route( "stores" )]
    public Task<IEnumerable<IStore>> GetStoresInfo ( string encryptedUserId ) {
        return _storeService.GetStoresInfo( EncryptionProvider.Decrypt( encryptedUserId ) );
    }
}

I am trying to test GetStoresInfo using Moq. 我正在尝试使用Moq测试GetStoresInfo This is all I have so far: 到目前为止,这就是我所拥有的:

[Fact]
public class Controller_Returns_List_Of_Stores()
{
    //Arrange
    var mockStoreService = new Mock<IStoreService>();
    var mockChartService = new Mock<IChartService>();
    var mockProductService = new Mock<IProductService>();



    var controller = new SelectController(mockChartService, mockProductService, mockStoreService);

    //Act
    //Assert
}

The new SelectController wont accept the 3 objects I am passing in due to being unable to convert from InService to InService . SelectController不会接受3个对象,我在路过由于暂时无法从转换InServiceInService Am I meant to be doing some more setup? 我是否打算进行更多设置? Or is it something to do with the return type being a Task? 还是与返回类型为Task有关?

Any help much appreciated. 任何帮助,不胜感激。

Call .Object on the mocks to pass the mocked objects. 在模拟对象上调用.Object以传递模拟对象。

 var controller = new SelectController(
     mockChartService.Object, 
     mockProductService.Object, 
     mockStoreService.Object
);

You also need to setup the mock object's behavior so that they function as expected when invoked. 您还需要设置模拟对象的行为,以使它们在被调用时能够按预期运行。

IEnumerable<IStore> fakeData = new List<IStore>();

mockStoreService
    .Setup(_ => _.GetStoresInfo(It.IsAny<string>()))
    .Returns(Task.FromResult<IEnumerable<IStore>>(fakeData));

Reference Moq Quickstart to get a better understanding of how to use the mocking framework. 参考Moq快速入门 ,以更好地了解如何使用模拟框架。

暂无
暂无

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

相关问题 如何使用或模拟 IWebJobsBuilder 对 Azure Function v2 进行集成测试? - How can I use or mock IWebJobsBuilder to do an integration test of my Azure Function v2? 如何在单元测试中模拟控制器上下文,以便我对字符串函数的部分视图有效? - How do I mock controller context in my unit test so that my partial view to string function works? 如何模拟owin的authorizationManager来测试我的api控制器 - How to mock owin's authorizationManager to test my api controller 如何模拟对Web API控制器单元的依赖关系测试? - How can mock dependencies to Web API controller unit test? 如何正确模拟我的controllercontext来测试ViewResult.ExecuteResult()? - How can I correctly mock my controllercontext to test ViewResult.ExecuteResult()? 我如何模拟Request.Url.GetLeftPart()以便我的单元测试通过 - How can I mock Request.Url.GetLeftPart() so my unit test passes 如何使用Mock C#在Service类中测试特定方法? - How I can test a particular method in my Service class using Mock C#? 如何模拟我的主Winform表单的实例来测试一些方法? - How can I mock up an instance of my main Winform form to test some methods? 如何使用Rhino模拟仅模拟测试对象中的一个值? - How can I use Rhino mocks to mock just one value in a test object? 如何在单元测试中使用 Mock 对象并仍然使用代码覆盖率? - How can I use Mock Objects in my unit tests and still use Code Coverage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM