moq

MOQ what happen if method setup intersect?

随声附和 提交于 2020-03-03 07:47:11
问题 What happen when two setup intersect or overlap if you prefer. For example, in the below scenario the setup overlap because obviously "aSpecificString" is also considered as any string. Interface ISomeInterface { int SomeMethod(string param); } [TestMethod] public void SomeClass_ShouldBehaveProperly_GivenSomeScenario() { var mock = new Mock<ISomeInterface>(MockBehavior.Strict); mock.Setup(m => m.SomeMethod("aSpecificString")) .Returns(100); mock.Setup(m => m.SomeMethod(It.IsAny<string>()))

How do I mock AddAsync?

不问归期 提交于 2020-03-01 05:55:46
问题 I'm writing unit test. For testing the method below, public async Task<Guid> CreateWebJobStatus(string blobId, Guid loggedInUserId, string loggedInUserEmail) { Guid webJobStatusId = Guid.NewGuid(); WebJobStatus newWebJobStatus = new WebJobStatus { WorkJobStatusId = webJobStatusId, TransactionId = Guid.NewGuid(), Status = (int)WebJobStatusEnum.PENDING, BlobId = blobId, UserId = loggedInUserId, UserEmail = loggedInUserEmail, }; await _dbContext.WebJobStatus.AddAsync(newWebJobStatus); await

How do I mock AddAsync?

巧了我就是萌 提交于 2020-03-01 05:55:46
问题 I'm writing unit test. For testing the method below, public async Task<Guid> CreateWebJobStatus(string blobId, Guid loggedInUserId, string loggedInUserEmail) { Guid webJobStatusId = Guid.NewGuid(); WebJobStatus newWebJobStatus = new WebJobStatus { WorkJobStatusId = webJobStatusId, TransactionId = Guid.NewGuid(), Status = (int)WebJobStatusEnum.PENDING, BlobId = blobId, UserId = loggedInUserId, UserEmail = loggedInUserEmail, }; await _dbContext.WebJobStatus.AddAsync(newWebJobStatus); await

Testing a method called many times in moq

我只是一个虾纸丫 提交于 2020-03-01 04:53:05
问题 I have an interface like so: Interface IWriteFile { string FileName {get;set;} void Open(); void WriteData(string dataToWrite); void Close(); } I want to test a class that will use this interface to populate a file. It will be calling WriteData a bunch of times and I just want to test the final output. Is there a way to introduce a new private field to the Mock object that will get appended to each time WriteData(Data) is called? I really just want to see what the file will look like at the

如何验证在Moq中没有调用该方法?

只谈情不闲聊 提交于 2020-02-27 23:45:15
如何验证在 Moq中未 调用该方法? 它有像AssertWasNotCalled这样的东西吗? 更新:从3.0版开始,可以使用新语法: mock.Verify(foo => foo.Execute("ping"), Times.Never()); #1楼 被盗: 约翰福斯特回答问题,“需要帮助更好地了解Moq” 您可能想要测试的一件事是,当65岁以上的人被传入方法时,不会调用pay方法 [Test] public void Someone_over_65_does_not_pay_a_pension_contribution() { var mockPensionService = new Mock<IPensionService>(); var person = new Person("test", 66); var calc = new PensionCalculator(mockPensionService.Object); calc.PayPensionContribution(person); mockPensionService.Verify(ps => ps.Pay(It.IsAny<decimal>()), Times.Never()); } #2楼 在测试后运行验证,该验证具有 Times.Never 枚举集。 例如 _mock.Object

什么是嘲弄?

梦想与她 提交于 2020-02-27 20:13:51
什么是嘲弄? 。 #1楼 Mock是一种方法/对象,以受控方式模拟真实方法/对象的行为。 模拟对象用于单元测试。 通常,测试中的方法会调用其中的其他外部服务或方法。 这些被称为依赖项。 一旦被模拟,依赖关系就像我们定义它们一样。 由于依赖项由模拟控制,我们可以轻松地测试我们编码的方法的行为。 这是单元测试。 模拟对象的目的是什么? 模拟vs存根 单元测试与功能测试 #2楼 SO上有很多答案,网上有关于嘲笑的好帖子。 你可能想要开始寻找的一个地方是Martin Fowler Mocks Are Not Stubs 的帖子,他讨论了许多嘲笑的想法。 在一个段落中 - Mocking是一种特殊技术,允许测试代码单元而不依赖于依赖性。 通常,模拟与其他方法的不同之处在于,用于替换代码依赖关系的模拟对象将允许设置期望 - 模拟对象将知道代码如何调用它以及如何响应。 你的原始问题提到了TypeMock,所以我在下面给出了答案: TypeMock是 商业 模拟 框架 的名称。 它提供了免费模拟框架(如RhinoMocks和Moq)的所有功能,以及一些更强大的选项。 你是否需要TypeMock是值得商榷的 - 你可以用免费的模拟库做大多数你想要的模拟,而且很多人认为TypeMock提供的功能通常会让你远离完美的封装设计。 正如另一个答案所说'TypeMocking'实际上并不是一个定义的概念

匿名类可以实现接口吗?

北慕城南 提交于 2020-02-26 16:39:28
是否可以使用匿名类型实现接口? 我有一段我想要工作的代码,但不知道该怎么做。 我有几个答案要么说不,要么创建一个实现接口的类构造新的实例。 这不是很理想,但我想知道是否有一种机制可以在界面上创建一个瘦动态类,这将使这个变得简单。 public interface DummyInterface { string A { get; } string B { get; } } public class DummySource { public string A { get; set; } public string C { get; set; } public string D { get; set; } } public class Test { public void WillThisWork() { var source = new DummySource[0]; var values = from value in source select new { A = value.A, B = value.C + "_" + value.D }; DoSomethingWithDummyInterface(values); } public void DoSomethingWithDummyInterface(IEnumerable<DummyInterface> values) {

What is use of Moq?

China☆狼群 提交于 2020-02-17 06:11:08
问题 I keep seeing this referred to on DotNetKicks etc... Yet cannot find out exactly what it is (In English) or what it does? Could you explain what it is, or why I would use it? 回答1: Moq is a mocking framework for C#/.NET. It is used in unit testing to isolate your class under test from its dependencies and ensure that the proper methods on the dependent objects are being called. For more information on mocking you may want to look at the Wikipedia article on Mock Objects. Other mocking

MOQ Error Expected invocation on the mock once, but was 0 times

核能气质少年 提交于 2020-02-03 05:54:59
问题 I am new to MOQ and I have read the Quickstart here. I am using MOQ v4.2.1402.2112. I am trying to create a unit test for updating a person object. The UpdatePerson method returns the updated person object. Can someone tell me how to correct this? I am getting this error: Moq.MockException was unhandled by user code HResult=-2146233088 Message=Error updating Person object Expected invocation on the mock once, but was 0 times: svc => svc.UpdatePerson(.expected) Configured setups: svc => svc

How to mock IDataReader to test method which converts SqlDataReader to System.DataView

泄露秘密 提交于 2020-02-03 04:31:06
问题 I'm new to Moq and I'm struggling to write Unit Test to test a method which converts SqlDataAdapter to System.DataView . This is my method: private DataView ResolveDataReader(IDataReader dataReader) { DataTable table = new DataTable(); for (int count = 0; count < dataReader.FieldCount; count++) { DataColumn col = new DataColumn(dataReader.GetName(count), dataReader.GetFieldType(count)); table.Columns.Add(col); } while (dataReader.Read()) { DataRow dr = table.NewRow(); for (int i = 0; i <