moq

Expectation on Mock Object doesn't seem to be met (Moq)

感情迁移 提交于 2019-12-10 15:20:04
问题 I'm experiencing some odd behavior in Moq - despite the fact that I setup a mock object to act a certain way, and then call the method in the exact same way in the object I'm testing, it reacts as if the method was never called. I have the following controller action that I'm trying to test: public ActionResult Search(string query, bool includeAll) { if (query != null) { var keywords = query.Split(' '); return View(repo.SearchForContacts(keywords, includeAll)); } else { return View(); } } My

How to raise an event in a test that uses Moq?

别等时光非礼了梦想. 提交于 2019-12-10 15:18:10
问题 Here is part of code implementation in parent class: handler.FooUpdateDelegate += FooUpdate(OnFooUpdate); protected abstract void OnFooUpdate(ref IBoo boo, string s); I have in test method mocked handler: Mock<IHandler> mHandler = mockFactory.Create<IHandler>(); This... mHandler.Raise(x => x.FooUpdateDelegate += null, boo, s); ...is not working. It says: System.ArgumentException : Could not locate event for attach or detach method Void set_FooUpdateDelegate(FooUpdate). I want to raise

moq callbase for methods that do not return value (void methods)

六月ゝ 毕业季﹏ 提交于 2019-12-10 15:11:02
问题 I am trying to mock my class that is under test, so that I can callbase on individual methods when testing them. This will allow me to test the method setup as callbase only, and all other methods (of the same class) called from within the test method will be mocked. However, I am unable to do this for the methods that do not return a value. The intellisense just doesn't display the option of callbase, for methods that do not return value. Is this possible? The Service Class: public class

All invocation on the mock must have a corresponding setup when setting string parameter

放肆的年华 提交于 2019-12-10 14:16:47
问题 I have a simple method I am testing. When I run the test I get the error "All invocation on the mock must have a corresponding setup" on the last line dataField.DefaultValue = orderNumber.ToString(); What would cause this? I am just setting a field. void IUtilities.SetOrderIdInDocumentMetaData(Document document, int orderNumber) { DataField dataField = null; if (document.DataFields.IsPresent(ORDER_ID) == false) { dataField = document.DataFields.Add(ORDER_ID, AppDefault: false, DocDefault:

How to mock HttpContext.User

邮差的信 提交于 2019-12-10 14:13:13
问题 I am working on a Asp.net MVC 5 project and I am trying to setup a mock to return a custom principal within a controller. I have search and tried different approach suggested but none of them works. I have a BaseController which all my controllers inherit from. The BaseController has a User property which return HttpContext.User in the getter. The HttpContext.user returns a value when called within the project but return a null when call from a unit test project. BaseController public class

How to mock context while unit testing code using VirtualPathUtility.GetAbsolute method

只愿长相守 提交于 2019-12-10 13:28:50
问题 I am running unit tests on code which uses VirtualParthUtility.GetAbsolute, but am having problems mocking the context for this to work. I've set up a mock context with Moq as follows private Mock<HttpContextBase> MakeMockHttpContext(string url) // url = "~/" { var mockHttpContext = new Mock<HttpContextBase>(); // Mock the request var mockRequest = new Mock<HttpRequestBase>(); mockRequest.Setup(x => x.ApplicationPath).Returns("/"); mockRequest.Setup(x => x.Path).Returns("/"); mockRequest

Async callback on mocked object not awaiting

六眼飞鱼酱① 提交于 2019-12-10 13:20:07
问题 I am attempting to mock a complicated situation for unit testing: _mockController = new Mock<IController>(); _mockController.Setup(tc => tc.Interrupt(It.IsAny<Func<Task>>())) .Callback<Func<Task>>(async f => await f.Invoke()); Where IController has a void method Interrupt(Func<Task>> f) , which queues some work to be done. My objects under test do call Interrupt() , and I can verify the call like so: _mockController.Verify(tc => tc.Interrupt(It.IsAny<Func<Task>>()), Times.Once); ...but when

How to mockup Entity Framework 6 With Moq & Autofixture

蹲街弑〆低调 提交于 2019-12-10 13:14:48
问题 I am using AutoMoq but I am kinda confused how to write my first unit test because of Entity Framework's (using EF6 and code first) dbContext // in service class(constructor) private readonly MyContext context; public PriceService(MyContext context) { this.context = context; } // following would be in nunit test method. var fixture = new Fixture().Customize(new AutoMoqCustomization()); var priceService = fixture.Create<PriceService>(); When I run the unit test it crashes at Ploeh.AutoFixture

How to mock protected virtual members with Rhino.Mocks?

六月ゝ 毕业季﹏ 提交于 2019-12-10 13:09:22
问题 Moq allows developers to mock protected members. I was looking for the same functionality in Rhino.Mocks but fail to find it. Here's an example from Moq Quick Start page how to mock protected method. // at the top of the test fixture using Moq.Protected() // in the test var mock = new Mock<CommandBase>(); mock.Protected() .Setup<int>("Execute") .Returns(5); // if you need argument matching, you MUST use ItExpr rather than It // planning on improving this for vNext mock.Protected() .Setup

Mocking Task.Delay

牧云@^-^@ 提交于 2019-12-10 12:55:50
问题 I have a method with the following line: await Task.Delay(waitTime).ConfigureAwait(false); I there a good strategy to avoid actually waiting the few seconds when unit testing and instead verify that we tried to wait a specific number of seconds. For instance, is there a way to inject an additional parameter into my method like in this (contrived) example where I inject a mocked object of a fictitious ITaskWaiter interface: // Arrange var mockWait = new Mock<ITaskWaiter>(MockBehavior.Strict);