Tag: moq

使用Moq模拟OpenXML

我应该如何测试以下GetWorksheetPart方法: public class ExcelDocument : IExcelDocument { private readonly string _filePath; public ExcelDocument(string filePath) { _filePath = filePath; } public WorksheetPart GetWorksheetPart(ISpreadsheetDocument excelDoc, string sheetName) { Sheet sheet = excelDoc.GetSheet(sheetName); if (sheet == null) { throw new ArgumentException( String.Format(“No sheet named {0} found in spreadsheet {1}”, sheetName, _filePath), “sheetName”); } return excelDoc.GetPartById(sheet.Id); } } IExcelDocument和包装器的SpreadsheetDocumentWrapper接口是: […]

如何将我的申请与会员服务分开?

我正在使用Castle Windsor的ASP.NET MVC 4项目。 其中一个控制器依赖于MembershipService: public class FooController : Controller { private readonly MembershipService MembershipService; public FooController( MembershipService membershipService ) { MembershipService = membershipService; } [Authorize( Roles = “Administrator” )] public ActionResult DoSomething() { var user = MembershipService.GetUser( User.Identity.Name ); // etc… } } 当我开始为这个控制器编写unit testing时(使用Moq),我遇到了一个问题: private Mock membershipServiceMock; [TestInitialize] public void MyTestInitialize() { membershipServiceMock […]

AutoFixture将PropertyData与多个条目和AutoData混合(使用AutoMoqCustomization)

我看过这两个类似的SO问题: AutoFixture:PropertyData和异构参数 AutoFixture CompositeDataAttribute不适用于PropertyDataAttribute 他们很棒,让我差不多到了那里。 但是这两个示例在发出的IEnumerable PropertyData中只使用了一个条目(即: yield return new object[] { 2, 4 }; – 请参阅: https : //stackoverflow.com/a/16843837/201308 )这有效,但它每当我想对多个对象[]测试数据进行测试时就会爆炸。 我有一整套想要发送的测试数据。 我在想这里的答案( https://stackoverflow.com/a/19309577/201308 )与我的需求类似,但我无法弄清楚。 我基本上需要AutoFixture为PropertyData的每次迭代创建一个sut实例。 一些参考: public static IEnumerable TestData { get { // totally doesn’t work return new List() { new object[] { new MsgData() { Code = “1” }, CustomEnum.Value1 }, new […]

使用Moq的unit testing工作单元和通用存储库模式框架

我正在使用Moq对使用工作单元和通用存储库的服务进行unit testing。 问题是在服务类中,当我在调试模式下运行测试时,_subsiteRepository始终为null。 我正在嘲笑服务类的设置 private readonly IRepository _subsiteRepository; public PlatformService(IUnitOfWork unitOfWork) { _subsiteRepository = unitOfWork.GetRepository(); } 以及正在测试的这个类中的方法。 问题是_subsiteRepository始终为null。 该方法不仅仅是这个,但这是相关的部分。 public async Task<IEnumerable> GetSubsites() { // Get Subsites var subsites = await _subsiteRepository .GetAll() .ToListAsync(); } 最后这是我正在运行的测试: private readonly Mock<IRepository> _subsiteRepository; private readonly Mock<IUnitOfWork> _unitOfWork; private readonly PlatformService _platformService; _subsiteRepository = new Mock<IRepository>(); _unitOfWork = new […]

为什么这个带有Expression <Func 的模拟不匹配?

我是模拟的新手,我试图做这个模拟示例: Repository.cs public class Repository : IRepository { public List GetForExpression(Expression<Func> expression ) { … //get person from orm using expression } } PersonService.cs public class PersonService { private IRepository _repository; public PersonService(IRepository repository) { _repository = repository; } public List GetAllPersonsWith18More() { var result = _repository.GetForExpression(x => x.Age > 18); return result; } } […]

使用It.IsAny ()的Moqunit testing失败

我正在使用Moq为项目编写unit testing,当我尝试validationDateTime属性是否被赋值时,其中一个测试失败。 这是我的validation(失败): _mockTaskContext.Verify(context => context.TaskQueue.AddObject(It.Is( task_queue => task_queue.TaskCode == (int)TaskCode.MyTask && task_queue.ClientID == ExpectedClientID && task_queue.JobNumber == It.IsAny() && task_queue.Requester == String.Empty && task_queue.JobStatus == (int)JobStatus.Submitted && task_queue.TimeQueued == It.IsAny() && task_queue.TimeStarted == new DateTime(1900, 1, 1) && task_queue.TimeStopped == new DateTime(1900, 1, 1) && task_queue.TaskParameters == expectedTaskParam )), Times.Once()); 如果我在task_queue.TimeQueued上注释掉期望值,则测试通过,而不对我的测试进行任何其他更改。 此外,如果我将TimeStarted或TimeStopped的要求new DateTime(1900, 1, […]

模拟entity framework长LINQ查询

有一个想要测试模拟entity framework的LINQ查询表达式(如下)。 要模拟我正在使用代码:使用模拟框架的entity framework测试这适用于其他查询,但不适用于此 var query = from vwPs in _reportingDbContext.VwAccountNumber join ps in _reportingDbContext.PaymentSummary on new { Key1 = vwPs.Id, Key2 = vwPs.AccountNumber, Key3 = true, Key4 = true } equals new { Key1 = ps.Id, Key2 = ps.AccountNumber, Key3 = ps.PaymentDate >= fromDateTime, Key4 = ps.PaymentDate < toDateTime } into viewJoinPayment from ps […]

使用Moq 永远不会设置validation属性

可能重复: Moq – 如何validation通过setter设置的属性值 我希望以下测试失败: public interface IObjectWithProperty { int Property { get; set; } } [TestMethod] public void Property_ShouldNotBeCalled() { var mock = new Mock(); mock.Object.Property = 10; mock.Verify(x => x.Property, Times.Never()); } 但是,即使在Verify之前在线路上清楚地访问了Property ,此测试也会通过。 所以看来Verify实际上意味着VerifyGet 。 我该如何validation永远不会设置属性?

entity framework6和Moq4:是否有可能让模拟的DbSet在其范围的持续时间内保留添加的数据?

背景: – 使用EntityFramework 6 – 使用Moq v4.2.1402.2112 – 使用DbFirst方法 我一直在关注EF6 Moq演练(可以在这里找到); 我一直想知道是否有可能让一个模拟的DbSet保留在其范围的持续时间内添加到它的数据? 例如: using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using System.Collections.Generic; using System.Data.Entity; using System.Linq; namespace TestingDemo { [TestClass] public class QueryTests { [TestMethod] public void GetAllBlogs_orders_by_name() { var data = new List { new Blog { Name = “BBB” }, new Blog { Name = “ZZZ” […]

System.NotSupportedException:非虚拟(在VB中可覆盖)成员上的无效设置

我使用Moq在unit testing中收到NotSupportedException错误消息 System.NotSupportedException:非虚拟(在VB中可覆盖)成员上的无效设置 unit testing代码: [TestMethod] public void TestEmailNotSentOut() { // … var dataAccess = new Mock(); var mockSetStock = new Mock<DbSet>(); mockSetStock.As<IQueryable>().Setup(m => m.Provider).Returns(stockList.Provider); mockSetStock.As<IQueryable>().Setup(m => m.Expression).Returns(stockList.Expression); mockSetStock.As<IQueryable>().Setup(m => m.ElementType).Returns(stockList.ElementType); mockSetStock.As<IQueryable>().Setup(m => m.GetEnumerator()).Returns(stockList.GetEnumerator()); dataAccess.Setup(m => m.Stocks).Returns(mockSetStock.Object); 这篇文章中的一个建议是将其标记为virtual ,但我不确定需要将哪些标记为虚拟? 错误发生在此行: dataAccess.Setup(m => m.Stocks).Returns(mockSetStock.Object);