English 中文(简体)
何时使用Mock的反馈与回归?
原标题:When to use Mock s Callback versus Return?

我认为,我已经非常直截了当地建立了搜索类型,并通过一个服务层和把一个领域清单归还的存放处。 搜索型除了在储存方法中建造一个表达树外,基本上从数据库中得出结果。 简单

接口:

public interface IDoNotSolicitRepo 
{
    IList<DNSContract> SelectWithCriteria(DNS_Search searchriteria); 
}

保管人执行服务:

public class DoNotSolicitService : BaseBLLService, IDoNotSolicitService
{
    private readonly IDoNotSolicitRepo repo;
    private readonly IPartnerService partnerService;
    private readonly IDoNotSolicitReasonService dnsReasonSvc;
    public DoNotSolicitService(IDoNotSolicitRepo _repo, IPartnerService _partnerSvc, IDoNotSolicitReasonService _dnsReasonSvc)
    {
        repo = _repo;
        partnerService = _partnerSvc;
        dnsReasonSvc = _dnsReasonSvc;
    }

    public ServiceResult<DNSContract> SelectWithCriteria(DNS_Search searchriteria)
    {
        var results = repo.SelectWithCriteria(searchriteria);

        return ReturnServiceResult(results);
    }
}

我在学习该项目的Moq时工作,如果Im本应使用“反馈”或“回归”的话,我可以 figure。 我拿到了这两个方面的总体观点,但现在似乎对我来说,两者都没有适当工作。

测试:

[Test]
public void SelectWithCriteria_FirstName()
{
    mockRepository.Setup(mr => mr.SelectWithCriteria(It.IsAny<DNS_Search>()))
        .Returns((IList<DNSContract> records) => new List<DNSContract>
                                                     {
                                                         new DNSContract {FirstName = "unit", LastName = "test"},
                                                         new DNSContract {FirstName = "moq", LastName = "setup"}
                                                     });

    dnsSvc = new DoNotSolicitService(mockRepository.Object, new PartnerServiceStub(), new DoNotSoicitReasonServiceStub());

    var result = dnsSvc.SelectWithCriteria(new DNS_Search { FirstName = "unit" });

    Assert.IsNotNull(result);
    Assert.IsTrue(result.Data.Any());
}

The error:

System.ArgumentException was unhandled by user code


 Message=Object of type  EP.Rest.Common.RestHelpers.DNS_Search  cannot be converted to type  System.Collections.Generic.IList`1[EP.Rest.Domain.Contracts.DNSContract] .

Now, I ve read that the Returns() method returns the type passed in, so I can see that s the cause of that error. But in the real world I want the different type returned. I ve attempted to create a callback delegate but none of that felt right.

最佳回答

仅靠lam倒。

.Returns(new List<DNSContract>());

你的原始论点是从你的手法中得出的,如果是从一个能够根据投入归还不同数据的来源中提取,就算出结果。

.Returns<IList<DNSContract>>(new List<DNSContract>(){...});
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签