English 中文(简体)
Moq: 验证抽象类别受保护的方法
原标题:Moq: Verifying protected method on abstract class is called

这是我的考验。

[TestClass]
public class RepositoryTests
{
    private APurchaseOrderRepository _repository;

    [TestInitialize]
    public void TestInitialize()
    {
        _repository = new FakePurchaseOrderRepository();
    }

    [TestMethod]
    public void RepositoryGetPurchaseOrdersForStoreCallsValidatePurchaseOrders()
    {
        var store = new Store();
        var mockRepo = new Mock<APurchaseOrderRepository>();
        mockRepo.Protected().Setup("ValidatePurchaseOrders", ItExpr.IsAny<List<PurchaseOrder>>());

        _repository.GetPurchaseOrders(store);

        mockRepo.Protected().Verify("ValidatePurchaseOrders", Times.Once(), ItExpr.IsAny<List<PurchaseOrder>>());
    } 
}

APurchaseOrder 资料库及其接口类似

public interface IPurchaseOrderRepository
{
    List<PurchaseOrder> GetPurchaseOrders(Store store);
}

public abstract class APurchaseOrderRepository : IPurchaseOrderRepository
{
    public abstract List<PurchaseOrder> GetPurchaseOrders(Store store);

    protected virtual bool ValidatePurchaseOrders(List<PurchaseOrder> purchaseOrders)
    {
        return true;
    }
}

我的面包

public class FakePurchaseOrderRepository : APurchaseOrderRepository
{
    public override List<PurchaseOrder> GetPurchaseOrders(Store store)
    {
        var purchaseOrders = new List<PurchaseOrder>();

        ValidatePurchaseOrders(purchaseOrders);

        return purchaseOrders;
    }
}

However, my test fails with:

Test method PreSwapTests.RepositoryTests.RepositoryGetPurchaseOrdersForStoreCallsValidatePurchaseOrders threw exception: Moq.MockException: Expected invocation on the mock once, but was 0 times: mock => mock.ValidatePurchaseOrders(It.IsAny())

Configured setups: mock => mock.ValidatePurchaseOrders(It.IsAny()), Times.Never No invocations performed.

我做了什么错误?

注:

  • Moq.4.0.10827

最新情况:

我认为,正是这一行程:mockRepo.Protected().Setup(“Validate PurchaseOrders”);,因为作为第二点,我需要增加参数,但我似乎无法纠正。

Update 2: Made some modifications, now it compiles, but isn t counting correctly...or something, error message and code are both updated above.

最佳回答

实现 我正在这样做所有错误,改变了我的物体,以配合这一检验。

    [TestMethod]
    public void RepositoryGetPurchaseOrdersForStoreCallsValidatePurchaseOrders()
    {
        var store = new Store();

        var mockPurchaseOrderProvider = new Mock<IPurchaseOrderProvider>();
        var mockPurchaseOrderValidator = new Mock<IPurchaseOrderValidator>();
        var purchaseOrderRepository = new PurchaseOrderRepository(mockPurchaseOrderProvider.Object, mockPurchaseOrderValidator.Object);

        mockPurchaseOrderValidator.Setup(x => x.ValidatePurchaseOrders(It.IsAny<List<PurchaseOrder>>()));

        purchaseOrderRepository.GetPurchaseOrders(store);

        mockPurchaseOrderValidator.Verify(x => x.ValidatePurchaseOrders(It.IsAny<List<PurchaseOrder>>()), Times.Once());
    }

我认为,这是一个更好的结构。

问题回答

这是因为<代码>Validate PurchaseOrders不在您的<编码>内。

储存库被宣布为<代码> 私人IPurchaseOrderRepository_repos;,因此只能看接口中的内容。





相关问题
Python: Binding method

In following example I am trying to bind a method object via types.MethodType(...). It does not seem to work. Any suggestions? Thanks in advance. import types class Base: def payload(self, *args)...

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Behaviour of final static method

I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance....

Setting the Page-Title in .Net using C# from a Class

Question, How do i set the title of a page from a class. Is it even possible? I can and have set the page title from a page itself and a usercontrol. Can I, How Do I do this via a class using C# ....

how to drag in the animation in iphone application?

Iphone application is using the static co-ordinates to reach at some points in the application,which is based on the button event. But what i want is when i drag the item than it should reach at some ...

热门标签