moq

How to Mock (with Moq) Unity methods

被刻印的时光 ゝ 提交于 2019-11-30 17:38:12
Extension methods are not good for testing (that's described here: Mocking Extension Methods with Moq , http://www.clariusconsulting.net/blogs/kzu/archive/2009/12/22/Howtomockextensionmethods.aspx ). But probably there are some solutions for mocking of Unity methods? In my case I have the following function: public class MyManager { public MyManager(IUnityContainer container) : base(container) { } public IResult DoJob(IData data) { IMyLog log = MyContainer.Resolve<IMyLog>(); ... use log.Id ... MyContainer.Resolve<...>();//usage for other purposes... } I want to be sure that 'DoJob' method will

Using Moq to Mock a Func<> constructor parameter and Verify it was called twice

爱⌒轻易说出口 提交于 2019-11-30 17:01:22
Taken the question from this article ( How to moq a Func ) and adapted it as the answer is not correct. public class FooBar { private Func<IFooBarProxy> __fooBarProxyFactory; public FooBar(Func<IFooBarProxy> fooBarProxyFactory) { _fooBarProxyFactory = fooBarProxyFactory; } public void Process() { _fooBarProxyFactory(); _fooBarProxyFactory(); } } I have a need to mock a Func<> that is passed as a constructor parameter, the assert that the func was call twice. When trying to mock the function var funcMock = new Mock<Func<IFooBarProxy>>(); Moq raises and exception as the Func type is not mockable

When to use a Mocking Framework?

六月ゝ 毕业季﹏ 提交于 2019-11-30 15:14:04
问题 So I am playing around with mocking frameworks (Moq) for my unit tests, and was wondering when you should use a mocking framework? What is the benefit/disadvantage between the following two tests: public class Tests { [Fact] public void TestWithMock() { // Arrange var repo = new Mock<IRepository>(); var p = new Mock<Person>(); p.Setup(x => x.Id).Returns(1); p.Setup(x => x.Name).Returns("Joe Blow"); p.Setup(x => x.AkaNames).Returns(new List<string> { "Joey", "Mugs" }); p.Setup(x => x.AkaNames

ASP.NET MVC - Unit testing RenderPartialViewToString() with Moq framework?

馋奶兔 提交于 2019-11-30 14:14:04
I'm using this helper method to turn my PartialViewResult into string and returning it as Json - http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/ My problem is that I'm using Moq to mock the controller, and whenever I run unit test that uses this RenderPartialViewToString() method, I got the "Object reference not set to an instance of an object." error on ControllerContext. private ProgramsController GetController() { var mockHttpContext = new Mock<ControllerContext>(); mockHttpContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("test"); mockHttpContext

How to Properly Test Controllers in ASP.net MVC that has database calls

烂漫一生 提交于 2019-11-30 14:12:58
问题 I am working on an ASP.net MVC 3.0 Application. I am using MSTest along with Moq for unit testing. I have written all the test methods for my controllers and ran those tests , which gave successful results. Now, I have a doubt whether I have properly made unit testing. Because, almost most of my controller actions contains database calls. I am not mocking them , I am mocking only Session and Request objects using Moq. Is it really necessary to mock database calls, since unit testing means

Moq Expect On IRepository Passing Expression

放肆的年华 提交于 2019-11-30 13:04:56
I am using this code to verify a behavior of a method I am testing: _repository.Expect(f => f.FindAll(t => t.STATUS_CD == "A")) .Returns(new List<JSOFile>()) .AtMostOnce() .Verifiable(); _repository is defined as: private Mock<IRepository<JSOFile>> _repository; When my test is run, I get this exception: Expression t => (t.STATUS_CD = "A") is not supported. Can someone please tell me how I can test this behavior if I can't pass an expression into the Expect method? Thanks!! This is a bit of a cheaty way. I do a .ToString() on the expressions and compare them. This means you have to write the

Mock HttpRequest in ASP.NET Core Controller

99封情书 提交于 2019-11-30 12:15:35
I'm building a Web API in ASP.NET Core, and I want to unit test the controllers. I inject an interface for data access, that I can easily mock. But the controller has to check the headers in the Request for a token, and that Request doesn't seem to exist when I simply instantiate the controller myself, and it is also get-only, so I can't even manually set it. I found lots of examples to mock an ApiController, but that isn't .NET core. Also many tutorials and examples of how to unit test .net core controllers, but none actually used the HttpRequest. I built an MCVE to demonstrate this:

Hirschmann9201375

只愿长相守 提交于 2019-11-30 12:13:16
Rechner K-130/3-E-10 ECKART 17.017.02-E3.125-196°(360°)/HW/M/SO FAVITEX 40W0-11298 P5 刀片 Indukey TKV-105-TB38V-MODUL-USB-US Ari-No:KV14008 InduKey K V 1 4 0 0 8 TKV-105-TB38V-MODUL-USB-US RECTUS SERIE--87 水接头 Wampfler 1CN33 Wampfler 2CN55 autotrol 150-1644A Electronicon E62.L95-253G10 Electronicon 118513 E62.L95-253G10 *MOQ=280pcs *packing unit: 10 pcs Electronicon 118513 E62.L95-253G10 *MOQ=280pcs *packing unit: 10pcs FAVITEX 40W0-11298 ZOLLERN ZHP3.32 1184399 festo 553111 Festo OT-FESTO039346 VMPA1-M1H-NU-PI anker cetriebe art-nr: 043416typ:CA43 GKN STROMAG NFF 63 227-90859 ORDER NO.287748

Verify the number of times a protected method is called using Moq

好久不见. 提交于 2019-11-30 11:35:21
In my unit-tests I'm mocking a protected method using Moq, and would like to assert that it is called a certain number of times. This question describes something similar for an earlier version of Moq: //expect that ChildMethod1() will be called once. (it's protected) testBaseMock.Protected().Expect("ChildMethod1") .AtMostOnce() .Verifiable(); ... testBase.Verify(); but this no longer works; the syntax has changed since then and I cannot find the new equivalent using Moq 4.x: testBaseMock.Protected().Setup("ChildMethod1") // no AtMostOnce() or related method anymore .Verifiable(); ... testBase

Unit Test a file upload, how?

我只是一个虾纸丫 提交于 2019-11-30 11:28:38
Using MVC3.NET I have a file upload method in a controller that works fine with the following signature public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> file) How can I unit test this with NUnit? I have looked around and everyone seems to point to Moq but I'm new to unit testing and cannot get Moq working. I have found interesting blogs such as this: http://danielglyde.blogspot.com/2011/07/tdd-with-aspnet-mvc-3-moq-and.html but am struggling to figure out how the same might be done to 'fake' a file upload, and am also wary that a lot on moq examples that I have managed to find