moq

How to mock non virtual methods?

情到浓时终转凉″ 提交于 2019-11-26 04:51:17
问题 [TestMethod] public void TestMethod1() { var mock = new Mock<EmailService>(); mock.Setup(x => x.SendEmail()).Returns(true); var cus = new Customer(); var result = cus.AddCustomer(mock.Object); Assert.IsTrue(result); } public class Customer { public bool AddCustomer(EmailService emailService) { emailService.SendEmail(); Debug.WriteLine(\"new customer added\"); return true; } } public class EmailService { public virtual bool SendEmail() { throw new Exception(\"send email failed cuz bla bla bla\

Mocking Asp.net-mvc Controller Context

雨燕双飞 提交于 2019-11-26 04:09:43
问题 So the controller context depends on some asp.net internals. What are some ways to cleanly mock these up for unit tests? Seems like its very easy to clog up tests with tons of setup when I only need, for example, Request.HttpMethod to return \"GET\". I\'ve seen some examples/helpers out on the nets, but some are dated. Figured this would be a good place to keep the latest and greatest. I\'m using latest version of rhino mocks 回答1: Using MoQ it looks something like this: var request = new Mock

How to mock the Request on Controller in ASP.Net MVC?

∥☆過路亽.° 提交于 2019-11-26 03:37:01
问题 I have a controller in C# using the ASP.Net MVC framework public class HomeController:Controller{ public ActionResult Index() { if (Request.IsAjaxRequest()) { //do some ajaxy stuff } return View(\"Index\"); } } I got some tips on mocking and was hoping to test the code with the following and RhinoMocks var mocks = new MockRepository(); var mockedhttpContext = mocks.DynamicMock<HttpContextBase>(); var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>(); SetupResult.For(mockedhttpContext

How do I use Moq to mock an extension method?

北战南征 提交于 2019-11-26 01:39:51
问题 I am writing a test that depends on the results of an extension method but I don\'t want a future failure of that extension method to ever break this test. Mocking that result seemed the obvious choice but Moq doesn\'t seem to offer a way to override a static method (a requirement for an extension method). There is a similar idea with Moq.Protected and Moq.Stub, but they don\'t seem to offer anything for this scenario. Am I missing something or should I be going about this a different way?

Mocking Extension Methods with Moq

我怕爱的太早我们不能终老 提交于 2019-11-26 00:59:08
问题 I have a preexisting Interface... public interface ISomeInterface { void SomeMethod(); } and I\'ve extended this intreface using a mixin... public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface someInterface) { // Implementation here } } I have a class thats calling this which I want to test... public class Caller { private readonly ISomeInterface someInterface; public Caller(ISomeInterface someInterface) { this.someInterface = someInterface; }

Assigning out/ref parameters in Moq

耗尽温柔 提交于 2019-11-26 00:53:58
问题 Is it possible to assign an out / ref parameter using Moq (3.0+)? I\'ve looked at using Callback() , but Action<> does not support ref parameters because it\'s based on generics. I\'d also preferably like to put a constraint ( It.Is ) on the input of the ref parameter, though I can do that in the callback. I know that Rhino Mocks supports this functionality, but the project I\'m working on is already using Moq. 回答1: Moq version 4.8 (or later) has much improved support for by-ref parameters:

How do I mock the HttpContext in ASP.NET MVC using Moq?

≡放荡痞女 提交于 2019-11-26 00:37:49
问题 [TestMethod] public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist() { var context = new Mock<HttpContextBase>(); var request = new Mock<HttpRequestBase>(); context .Setup(c => c.Request) .Returns(request.Object); HomeController controller = new HomeController(); controller.HttpContext = context; //Here I am getting an error (read only). ... } my base controller has an overrride of the Initialize that get\'s this requestContext. I am trying to pass this along but I am not

Mocking Extension Methods with Moq

馋奶兔 提交于 2019-11-25 21:52:36
I have a preexisting Interface... public interface ISomeInterface { void SomeMethod(); } and I've extended this intreface using a mixin... public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface someInterface) { // Implementation here } } I have a class thats calling this which I want to test... public class Caller { private readonly ISomeInterface someInterface; public Caller(ISomeInterface someInterface) { this.someInterface = someInterface; } public void Main() { someInterface.AnotherMethod(); } } and a test where I'd like to mock the interface and