moq

How to mock ModelState.IsValid using moq framework

I am checking that the ModelState.IsValid in my action method that create the Employee [HttpPost] public virtual ActionResult Create(EmployeeForm employeeForm) { if (this.ModelState.IsValid) { try { IEmployee employee = this._uiFactoryInstanc...

Should Mock<SomeClassType>.SetupAllProperties() cause properties to return the values they are assigned?

When I use SetupAllProperties on a Mock, it works as expected: /// <summary> /// demos SetupAllProprties on an interface. This seems to work fine. /// </summary> [Test] public void Demo_SetupAllProperties_forAnInterface() { var mock = new Mock<IAddress>(); mock.SetupAllProperties(); var stub = mock.Object; stub.City = ...

Compile error when trying to mock a generic method with Moq

I have a method I'd like to mock: public interface IServiceBus { void Subscribe<T>(ISubscribeTo<T> subscriber) where T : class; } For the sake of this example, T can be something called SomeType. Now, I'd like to mock this, like so: var mockServiceBus = new Mock<IServiceBus>(); mockServiceBus.Setup(x => x.Subscribe(It.IsAny<ISubs...

Why does my route test fail but the route works when running the application?

I've got the default route set-up: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Shortie", // Route name "{controller}/{id}", // URL with parameters new { controller = "Ettan", action = "Index", id = "id" } // P...

Mocking Asynchronous Calls in Silverlight WCF Proxy using Moq

I have a typical Silverlight application with a WCF service and I am using slsvcutil.exe to generate the standard client proxy to communicate with the web service. I am trying to write unit tests and I'm attempting to use the Silverlight Unit Testing framework and Moq to mock the proxy and remove the service dependency for testing. I am...

MOQ 4.0: The type initializer for 'Moq.Mock`1' threw an exception.

I'm getting the exception The type initializer for 'Moq.Mock`1' threw an exception. using Moq 4.0 I've checked around on a couple of forums and they allude to using the Moq-NoCastle version. I've tried both this and version in the Moq folder. Both with the same result. I've got a solution with 2 projects, one for my interface...

Newbie question about mocking and moq framework

I've been using Moq framework in c# for mocking in unit tests however there is one thing I dont complete understand yet. I have this line of code var feedParserMock = new Mock<ApplicationServices.IFeedParser>(); feedParserMock.Setup(y => y.ParseFeed(csv)).Returns(items).Verifiable(); The second line does it mean it will only return th...

Problem with TempData and Faked HttpContext using ASP.NET MVC

Hi Everyone, I am working with a faked HttpContext (code provided in the end) and probably I am missing something because I can't access TempData collection (forth line of SetFakeControllerContext method). Every time I try I get this error message: 'controller.TempData' threw an exception of type 'System.AccessViolationException' The ...

How to make VS Unit Test show the Error Message from exceptions other than UnitTestAssertException?

I'm using VS Unit Testing Framework and Moq. When a Moq verification fails, I'll get a Moq.MockException. In the Test Results window, instead of showing the helpful message inside the exception, it just says "Test method XXX threw exception: ..." Is there a way to tell the VS Unit Test framework always display the message of exceptions...

Does Moq replace standard VS 'Assert' testing?

I am trying to understand how to use Moq, however I am having some confusion about what Moq should be used for. My understanding is that mock framework are used for generating objects which would be difficult to create under normal circumstances. The examples I have seen of Moq however seem to not only create Moq object, but also provide...

Verify that a base protected method is called with Moq 3.1

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

Microsoft.Web.Mvc.LinkBuilder.BuildUrlFromExpression failing when testing Asp.net MVC app

So I'm trying to unit-test a controller method. I'm using MSTest in VS 2010, and Moq 3.1 Test method: [TestMethod] public void TestAccountSignup() { var request = new Mock<HttpRequestBase>(); var context = new Mock<HttpContextBase>(); AccountController controller = new AccountController(); ...

How can I create and use a partial stub (in MoQ) without being tied to the concrete implimentation?

I have code that uses MoQ to create a partial stub. I'd prefer to interact with the interface instead of the concrete implementation so that I won't have to modify the unit test if I have a different implementation of the interface. So for example, I have a factory method such as: private Mock<ISomeInterface> ISomeInterfaceStubFactory...

How to mock a method which also belongs to the target class itself?

Let's say we are testing a class C which has 2 methods M1 and M2 where M1 calls M2 when executed. Testing M2 is ok, but how can we test M1? The difficulty is that we need to mock M2 if I'm not misunderstanding things. If so, how can we mock another method while testing a method defined in the same class? [Edit] Class C has no base cla...

Moq a function but don't have access to the arguments

I have a class that constructs a new object to add to the internal state of an object I am mocking... something like public class foo { public bar raz; public foo(bar raz) { this.raz = raz; } public void InsertItem() { raz.Insert(new FooBar()); } ...

Moq - Need mocked function to return value passed in

I have a mock that i have setup like this. I need to return the same value that was passed in to .CreatePersonName mock.Setup(m => m.CreatePersonName(It.IsAny<PersonName>())) .Returns(// what do i put here?); ...

C# + Mock service layer?

Hello there, I have just started playing with unit testing / mocks using Moq, and ran into a problem.. I have a Service layer named "CustomerService" which have following code: public interface ICustomerService { Customer GetCustomerById(int id); } public class CustomerService : ICustomerService { private IRepository<Customer>...

How to mock a method that returns an int with MOQ

I have a class that does some retrieving of contents, and it has a method that requires some inputs (filters) before retrieving it. One of the "input" calls another method, which basically returning an int, how do I mock it using MOQ? Here's an example: namespace MyNamespace { public class ConfigMetaDataColumns : MyModel { ...

How to Moq a Method that Have parameters and have to do something with a list

I have set a test using Moq 3.0. I need to test a method that will change some value in that database. I am using ASP.NET MVC so I want to test my controller. I setup this // Generate an implementer of IProductsRepository at runtime using Moq var mockTareasRepos = new Mock<IRepository>(); mockTareasRepos.Setup(x => x.ExecutedTask).R...

Moq in Microsoft Data Access Application Block

I just downloaded the latest release of the source code to the Microsoft Enterprise Library. When I tried to build the solution in Visual Studio 2010, I get the following error: The type or namespace name 'Moq' could not be found (are you missing a using directive or an assembly reference?) What is Moq and where can I find th...