English 中文(简体)
单位测试
原标题:Unit test with Moq

我正在利用莫克进行单位测试,我正试图撰写我的第一份单位测试。 我的层次是“主计长=和特;服务=和特;保存”。

(我采用团结和存放方式)

每当我进行单位测试时,实际价值始终为: <0>0> > > > / 编号 ,如:>_service.GetEquipStates().Count() = 0 <>。 我不知道我在哪里做错事。 建议。

我的单位试验守则如下:

private ITestService _service;
private Mock<ITestRepository> RepositoryMoc; 

[TestInitialize]
public void Initialize() { 
    RepositoryMoc= new Mock<ITestRepository>(); 
    _service = new TestService(RepositoryMoc.Object) 
}
[TestMethod]
public void GetEquipmentState() {   
    var stateList = new[] { new State { ID = 1, Desc= "test" } };         
    RepositoryMoc.Setup(es => es.GetStates(true)).Returns(stateList );     
    Assert.AreEqual(1, _service.GetStates().Count());           
}
最佳回答

你们的设立是为了真正实现直截了当的方法。

RepositoryMoc.Setup(es => es.GetStates(true)).Returns(stateList);

但是,你在《阿森特宣言》中发出的呼吁是,一种没有参数的“让国家”方法。 格塔尼的方法是用缺省参数宣布的,还是你必须运作(一个有ool参数,一个没有参数)?

就像这样说,你就这样呼吁了,它应该发挥作用。

Assert.AreEqual(1, _service.GetStates(true).Count());           
问题回答

我把你的守则复制到我的解决方案之一,并用测试通行证进行罚款。

    private Mock<IAccessor> RepositoryMoc;
    private Controller _service;

    [TestMethod]
    public void TestMethod()
    {
        // Arrange
        _service = new Controller();
        RepositoryMoc = new Mock<IAccessor>();

        _service.Accessor = RepositoryMoc.Object;

        var stateList = new[] { new State { ID = 1, Desc = "test" } };
        RepositoryMoc.Setup(es => es.GetStates(true)).Returns(stateList);

        // Act & Assert
        Assert.AreEqual(1, _service.GetStates().Count());           
    }

守则是否与你的解决办法完全一样?





相关问题
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, ...

热门标签