moq

Moq: Invalid callback. Setup on method with parameters cannot invoke callback with parameters

你离开我真会死。 提交于 2019-12-03 22:53:22
I am trying to use moq to write a unit test. Here is my unit test code var sender = new Mock<ICommandSender>(); sender.Setup(m => m.SendCommand(It.IsAny<MyCommand>(), false)) .Callback(delegate(object o) { var msg = o as MyCommand; Assert.AreEqual(cmd.Id, msg.Id); Assert.AreEqual(cmd.Name, msg.Name); }) .Verifiable(); SendCommand takes an object and optional boolean parameter. And MyCommand derives from ICommand. void SendCommand(ICommand commands, bool idFromContent = false); When the test runs, I see the error System.ArgumentException : Invalid callback. Setup on method with parameters

Mock a method of the subject under test in Moq?

若如初见. 提交于 2019-12-03 22:20:45
I want to test method A of my class, but without calling the actual method B which is normally called by A. That's because B has a lot of external interactions I don't want to test for now. I could create mocks for all the services called by B, but that's quite some work. I'd rather just mock B and make it return sample data. Is that possible to do with Moq framework? It is, with a catch! You have to make sure method B is virtual and can be overriden. Then, set the mock to call the base methods whenever a setup is not provided. Then you setup B , but don't setup A . Because A was not setup,

MOQ - check a method is called with only specific parameters

微笑、不失礼 提交于 2019-12-03 21:31:25
I have a method that assigns some values to a dictionary, and in my Moq test I want to ensure that it is only called with a specific set of parameters (i.e., strings). I've tried a variety of things - Setup, SetupSet, VerifySet, etc. They all let me ensure that a method is called with the parameters I want, which is fine. But how do I verify that it wasn't called with anything else? None of the above methods will catch this scenario. I tried to use It.IsInRange to specify which params were ok, but that only accepts ints so for a dictionary lookup is not much use. Thanks, Alex You can do this

How to test a COM dependent object in C#

半腔热情 提交于 2019-12-03 20:59:44
问题 I´m trying to do TDD with an object that has a dependency on a COM Interface. I though about mocking the COM interface, while doing development testing, and do it real on the integration tests. However, I cannot mock the COM interface, I tried with Moq, and it throws an exception: System.TypeLoadException was unhandled by user code Message=Could not load type 'Castle.Proxies.iTunesAppProxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. The

How to mock a web service

我的梦境 提交于 2019-12-03 19:03:22
问题 Do I have to rewrite my code to do this into an interface? Or is there an easier way? I am using Moq 回答1: What I usually do is build a wrapper or an adapter around my web service and just mock that. for instance: public class ServiceAdapter: IServiceAdapter { public void CallSomeWebMethod() { var someService = new MyWebService(); someService.SomeWebMethod(); } } Then I just stub the service adapter. [Test] public void SomeMethod_Scenario_ExpectedResult() { var adapterMock = new Mock

Unit Testing Mock/Stub definitions in Moq

岁酱吖の 提交于 2019-12-03 17:33:16
问题 Any reading or advice I've been given on Unit Testing has always suggested a distinct difference between the definition of a Mock and a Stub. My current understanding of these definitions are as follows Mock: A fake which will be used in your test to make a final assertion Stub: A fake which will be used in your test to isolate a dependency but not be asserted However, Moq appears to only allow the creation of Mocks. The Stub namespace in the framework appears to be depreciated with

How can I use Mock Objects in my unit tests and still use Code Coverage?

狂风中的少年 提交于 2019-12-03 16:52:12
问题 Presently I'm starting to introduce the concept of Mock objects into my Unit Tests. In particular I'm using the Moq framework. However, one of the things I've noticed is that suddenly the classes I'm testing using this framework are showing code coverage of 0%. Now I understand that since I'm just mocking the class, its not running the actual class itself....but how do I write these tests and have Code Coverage return accurate results? Do I have to write one set of tests that use Mocks and

Mocking HttpRequest in ASP.NET 4.0

匆匆过客 提交于 2019-12-03 16:16:42
I've seen a lot of similar threads but none that actually address my particular situation. I'm writing unit tests in ASP.NET 4.0 web application (ASP.NET Forms, not MVC). There are several spots in the code where I call the ServerVariables collection to call variables like REMOTE_ADDR . Since my unit tests do not actually initiate HttpRequests when executing my code, things like ServerVariables are Null and therefore error when I try to call HttpContext.Current.Request.ServerVariables("REMOTE_ADDR") All the solutions I've found to address this issue refer to MVC and so they assume that

How do I mock controller context in my unit test so that my partial view to string function works?

落花浮王杯 提交于 2019-12-03 16:16:05
问题 I am attempting to create a unit test for my controller, but the action I am testing uses a partial view to string function which doesn't want to work in my tests. private string RenderPartialViewToString(string viewName, object model = null) { if (string.IsNullOrEmpty(viewName)) viewName = ControllerContext.RouteData.GetRequiredString("action"); ViewData.Model = model; using (System.IO.StringWriter sw = new System.IO.StringWriter()) { ViewEngineResult viewResult = ViewEngines.Engines

multiple parameters call back in Moq

我的未来我决定 提交于 2019-12-03 16:10:43
问题 Can someone please look at the code below and see what's wrong? [TestInitialize] public void SetupMockRepository() { var memberId = "34345235435354545345"; var title = "test"; var url = "dafdsfdsfdsfdsafd"; _mockPropertySearchRepository = new Mock<IPropertySearchRepository>(MockBehavior.Strict); _mockPropertySearchRepository.Setup(p => p.SaveSearchURL(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback<string,string,string>((id,t,u) => ).Returns(new SavedSearchReturnResult()