moq

Moq an indexed property and use the index value in the return/callback

泄露秘密 提交于 2020-01-11 08:23:10
问题 I want to moq a property that has an index, and I want to be able to use the index values in the callback, the same way you can use method arguments in the callback for moq'd methods. Probably easiest to demonstrate with an example: public interface IToMoq { int Add(int x, int y); int this[int x] { get; set; } } Action<int, int> DoSet = (int x, int y) => { Console.WriteLine("setting this[{0}] = {1}", x, y); throw new Exception("Do I ever get called?"); }; var mock = new Mock<IToMoq>

Mock authenticated user using Moq in unit testing

守給你的承諾、 提交于 2020-01-11 01:08:19
问题 How we can mock the authenticated user using Moq framework. Form Authentication used. I need to write unit tests for the action below public PartialViewResult MyGoals() { int userid = ((SocialGoalUser)(User.Identity)).UserId; var Goals = goalService.GetMyGoals(userid); return PartialView("_MyGoalsView", Goals); } I need to mock the value for userid here 回答1: I have used something like that, maybe it helps you: var controllerContext = new Mock<ControllerContext>(); var principal = new Moq.Mock

Moq Example using out and ref needed

旧街凉风 提交于 2020-01-10 04:52:04
问题 I am trying to build a test against some legacy method that implement out parameters. Could you give me an example how to do this? 回答1: Just assign the out or ref parameter from the test. Given this interface: public interface ILegacy { bool Foo(out string bar); } You can write a test like this: [TestMethod] public void Test13() { string bar = "ploeh"; var legacyStub = new Mock<ILegacy>(); legacyStub.Setup(l => l.Foo(out bar)) .Returns(true); Assert.IsTrue(legacyStub.Object.Foo(out bar));

MVC3 unit testing response code

痴心易碎 提交于 2020-01-09 09:49:39
问题 I have a controller within MVC3 which needs to return a response code 500 if something goes wrong. I am doing this by returning a view object and setting http response code to equal 500 (I have checked this in firebug and all is working great). public ActionResult http500() { ControllerContext.HttpContext.Response.StatusCode = 500; ControllerContext.HttpContext.Response.StatusDescription = "An error occurred whilst processing your request."; return View(); } The problem I have now is I need

SetupSequence in Moq

回眸只為那壹抹淺笑 提交于 2020-01-09 06:35:39
问题 I want a mock returns a 0 the first time, then returns 1 anytime the method was called. The problem is that if the method is called 4 times, I should write that : mock.SetupSequence(x => x.GetNumber()) .Returns(0) .Returns(1) .Returns(1) .Returns(1); otherwise the method returns null. Is there any way to write that the next times the method was called after the first time, the method returns 1 ? Thank you Is it good to have more "operators" for SetupSequence ? If you think YES you can vote :

Why does the property I want to mock need to be virtual?

独自空忆成欢 提交于 2020-01-09 03:23:35
问题 I'm doing some unit testing, and mocking some properties using Moq . Now, this is a Controller test (ASP.NET MVC 3). My Controllers derive from an abstract controller, called AbstractController . This controller has a dependency on the Http Context (in order to do things like theming, domain-specific logic based on HTTP HOST headers, etc). This is done via a property called WebSiteSettings : public abstract class AbstractController : Controller { public WebSiteSettings WebSiteSettings { get;

Verify that a base protected method is called with Moq 3.1

不想你离开。 提交于 2020-01-07 04:09:24
问题 I have a class that inherits from an abstract base class. I am trying to verify that a specified protected method in the base class is called twice and I'd like to verify that the parameters passed are specific values (different for each call). I was hoping that I'd be able to use Protected with either Expect or Verify , but seemingly I've missed what can be done with these methods. Is what I'm attempting possible with moq? UPDATE: An example of what I'm trying to do: class MyBase { protected

Verify that a base protected method is called with Moq 3.1

安稳与你 提交于 2020-01-07 04:09:12
问题 I have a class that inherits from an abstract base class. I am trying to verify that a specified protected method in the base class is called twice and I'd like to verify that the parameters passed are specific values (different for each call). I was hoping that I'd be able to use Protected with either Expect or Verify , but seemingly I've missed what can be done with these methods. Is what I'm attempting possible with moq? UPDATE: An example of what I'm trying to do: class MyBase { protected

Moq Verify number of calls works if test called alone, but not if called along other tests

半腔热情 提交于 2020-01-07 02:56:07
问题 I am working on a C# application, which uses Moq for testing. I am trying to count the number of times a method is being called. The following piece of code is used to initialise the test objects: private Mock<IService> _serviceMock; [TestInitialize] public void Initialize() { MockServices(); Rule rule = new Rule( // Parameters ); RuleItem = new RuleItem(rule); } private void MockServices() { UnityContainer unityContainer = new UnityContainer(); _serviceMock = new Mock<IService>();

c# mock return base method

点点圈 提交于 2020-01-06 07:14:31
问题 I am writing a unit test case with NUnit framework to test our code. The code has referenced to 3rd party libraries like below: class MyClass: BaseClass { public void override FunctionA() { var a = BaseFunctionB(); } } we don't have sourcecode for BaseClass , but the BaseFunctionB is non-virtual. I was trying to Setup(x=> x.BaseFunctionB()).Reteruns("my values"); but it doesn't allow. I just want to test the FunctionA in MyClass , I don't care whether it's correct in BasefunctionB . How to