moq

How to set-up mock object to return one result at first call and Exception for the rest

I'm using Moq library. I'm mocking up an instance that does all regular CRUD functionality. I would like to set it up in a way to allow only one Delete(x) call over some object and all consecutive calls to Delete(x) of the same object should return an Exception. My Delete() method returns void. How do I do that? Some code mock = new ...

Using MOQ to test controller

I'm having trouble writing a unit test for one of my controller actions. Here's the details. This view is strongly typed: Inherits="System.Web.Mvc.ViewPage<IEnumerable<Request>>" Here is the method in the controller under test: // GET: /Request/List public ActionResult List() { return View("List", r...

MOQ: Returning value that was passed into a method

I have a method on an interface: string DoSomething(string whatever); I want to mock this with MOQ, so that it returns whatever was passed in - something like: _mock.Setup( theObject => theObject.DoSomething( It.IsAny<string>( ) ) ) .Returns( [the parameter that was passed] ) ; Any ideas? ...

The Purpose of Mocking

What is the purpose of mocking? I have been following some ASP.NET MVC tutorials that use NUnit for testing and Moq for mocking. I am a little unclear about the mocking part of it though. ...

How do you mock class with readonly property?

Any idea in Moq for a class with readonly modifier : Class myClass { private int id; public int Id{ get {return id;}} public myClass(int id) { this.id = id } } I was trying to mock this object: var myMock= new Mock<myClass>(); myMock.SetupGet(m => m.ID).Return(555); It returned me error: System.ArgumentException: I...

What would this Moq code look like in RhinoMocks

Hey people... trying to get my mocking sorted with asp.net MVC. I've found this example on the net using Moq, basically I'm understanding it to say: when ApplyAppPathModifier is called, return the value that was passed to it. I cant figure out how to do this in Rhino Mocks, any thoughts? var response = new Mock<HttpResponseBase>(); re...

How can I mock ActivityExecutionContext

This class is sealed but I need to mock it using Moq for use in a CRM workflow development for calling the method: protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) How can I do this or get around the problem by creating an instance of ActivityExecutionContext (which has no public constr...

Need Help understanding this code.

Hi I am trying to learn unit testing. I am trying to unit test some Memembership stuff I am making in asp.net mvc 1.0. I been following a book on MVC and I am confused about some stuff that hopefully someone can clear up for me. I am using Nunit and Moq for my frameworks. Question 1: public AuthenticationController(IFormsAuthentica...

How do I setup this(Moq Setup)

Hi I want to test my part of code that returns the users password question. So I have made a mockup of the Membership provider using MOQ. I don't think I need to show you the actual code just the test part of it. // Arrange var membershipMock = new Mock<MembershipProvider>(); membershipMock.Setup(m => m.GetUser(...

How can I generate ASP.NET MVC URLs inside a unit test project?

How can I generate the URLs corresponding to the controller, action, and parameters (for a given ASP.NET MVC project) in another project (a class library used for testing)? All I have found so far is HtmlHelper.GenerateRouteLink, but didn't find yet how to pass the correct request context and route collection. ...

What am I doing wrong this time with Moq?

Hi I am having problems with moq again and not sure what I did wrong this time. So I am going through the debugger step by step and I notice even though in my Mock I set ResetPassword to return "hey it does not seem to Here is part of my unit test: var membershipMock = new Mock<MembershipProvider>(); var user = new Mo...

Assigning out/ref parameters in Moq

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 ...

Recording interaction on an inflection point using mocking framework. Moq

Updated version of question Hi. My company has a few legacy code bases which I hope to get under test as soon as they migrate to .NET 3.5. I have selected Moq as my Mocking framework (I fell in love with the crisp syntax immediately). One common scenario, which I expect to see a lot of in the future, is where I see an object which i...

Need Help understand Moq better.

Hi I been looking at the Moq documentation and to me the comments are too short for me to understand each of things it can do. First thing I don't get is It.IsAny<string>(). //example using string Is there an advantage of using this over just putting some value in? Like I know people say use this if you don't care about the value but...

How to Unit Test a custom ModelBinder using Moq?

I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that I created. The ModelBinder I'm trying to Unit Test is the JsonDictionaryModelBinder that I posted here. The problem I'm having is getting the Mocking all setup using Moq. I keep getting Null Exceptions due to the HttpContextBase not being Mocked correct...

Does Moq ignore namespaces when setting up mocks?

The second assertion never executes in the unit test below: namespace Foo { public class MyClass { } } namespace Bar { public class MyClass { } } namespace Quux { public interface IRepo { object Get<T>() where T : new(); } } namespace Tests { [TestFixture] public class MyTests { private Mock<...

Verify that either one method or the other was invoked in a unit test

Example: public bool Save(MyObj instance) { if (instance.IsNew) { this.repository.Create(instance); } else { this.repository.Update(instance); } } How do I create a test in Moq that verifies: that a property IsNew is being read that either Create() or Update() has been invoked ...

Unit tests with mock verifies

I have a unit test that creates a mock calls my method to be tested (also injecting my mock) asserts method results verifies mock calls When mock calls don't verify as expected I get an exception, thus failing a test. How should I correctly call this verifies? Should I just be calling // verify property get accessor call m.VerifyGet...

Proper application of Mock objects in Unit Testing

I've got a PresenterFactory that creates Presenter classes based on a Role parameter. Specifically, the Role parameter is an external class which I cannot control (IE 3rd party.) My factory looks something like this: public class PresenterFactory { public Presenter CreatePresenter(Role role, ...) { if (role.IsUserA("Manage...

Set a ServerVariable value when mocking HttpRequest using Moq?

I am mocking an HttpRequest object using Moq for unit testing in ASP.NET MVC. I need to set one of the ServerVariables (LOGON_USER) in the request. Is this possible? I have tried using the following method, but I get an exception because the ServerVariables collection is non-overridable. request.SetupGet(req => req.ServerVariables["...