moq

Moq a MEF Import?

I have a class A which has the following: public class A { [Import(typeof(IMyService)] public IMyService MyService { get; set; } public A() { CompositionInitializer.SatisfyImports(this); } public void DoWork() { //Blah MyService.DoIt(); //Blah } } And a Test to test this (seper...

Moq - verify that no methods were called

This is a unit-test from one of my controllers in an ASP.NET MVC project, using NUnit and Moq: [Test] public void Create_job_with_modelstate_errors_fails() { var job = new JobDto(); this.controller.ModelState.AddModelError("", ""); ActionResult result = this.controller.Create(job); this.jobS...

MVC Testing using a mocking framwork (Moq)

Hi I'm using Moq to help in testing my ASP.NET MVC2 application. Problem: ArgumentException was unhandled by user code. Unable to obtain public key for StrongNameKeyPair This code has been adapted from Scott Hanselman's NerdDinner1. HomeController CreateHomeControllerAs(string userName) { var mock = new Mock<Controller...

Mock objects - Setup method - Test Driven Development

I am learning Test Driven Development and trying to use Moq library for mocking. What is the purpose of Setup method of Mock class? ...

Mocking wcf in silverlight

I thought that I could create a WCF in and call it in Silverlight. I would inject an interface to the WCF. Then in my unit test I would mock the wcf.... However when I actually got to do this I notice that the interface does not actually have the methods that I am calling. ie myWCF.myfunctionCompleted(myhandler); myWCF.myfunctionAsy...

ControllerContext.IsChildAction invocation failed with mock behavior Strict. All invocations must have a setup.

I am toying around to learn how to unit test ASP.NET MVC controller actions. Specifically I'm trying to mock the ControllerContext so that I can test an action that accesses HttpContext.Current.User.Identity.Name. I'm using Moq. Things were going pretty well until I turned on MockBehavior.Strict. I knew that this would throw an excep...

Cannot convert from HttpContextBase to HttpContextBase

With the following code (using Moq 4.0.10501.6): HomeController controller = new HomeController(); ActionResult result = _controller.Index(); Mock<HttpResponseBase> response = new Mock<HttpResponseBase>(); Mock<HttpContextBase> httpContext = new Mock<HttpContextBase>(); httpContext.Setup(x => x.Response).Returns(response.Object); Moc...

How to test a COM dependent object in C#

Hi, 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 us...

Using Moq's VerifySet in VB.NET

I have a function that updates a user in the asp.net membership provider. <AcceptVerbs(HttpVerbs.Post)> Public Function EnableUser(ByVal id As String) As JsonResult Dim usr As StargatePortalUser = _membershipService.GetUser(id, Nothing) usr.IsApproved = True _membershipService.UpdateUser(usr) Dim response As New AjaxResponse(usr...

Moq to Rhino - fake partial repository

I got this really cool Moq method that fakes out my GetService, looks like this private Mock<IGetService<TEntity>> FakeGetServiceFactory<TEntity>(List<TEntity> fakeList) where TEntity : class, IPrimaryKey, new() { var mockGet = new Mock<IGetService<TEntity>>(); mockGet.Setup(mock => mock.GetAll()).Returns(fakeList); mockG...

How to access a mock object in another mock object in Moq

Say I have an interface that has as property another interface. Something like: interface IOne { ITwo Two { get; set;} } And I create a mock for ITwo and then create a mock for IOne using Mock<ITwo>.Object. How can I raise an event on ITwo when I only have a reference to IOne or Mock<IOne>? I need a reference for Mock<ITw...

Moq: how to verify a function that takes in a object created within a method

Hi all: I have the following method; public class MyClass { public Repository UserRepository { get; set; } public void CreateUser(Message someMsg) { if (someMsg.CanCreate) { var obj = new object(); UserRepository.Save(obj) } } } In my test case, I used Moq to mock out the Obj...

Moq controller tests with repeated setup

I am getting started on the Moq framework and absolutely love it. I am writing some controller tests that have several services and interfaces to Arrange my controller for the test. I'd love to modularize it a bit more, and thought this would be a trivial task, but it turns out to be a bit trickier than I thought. Here is one simple un...

Mocking HttpRequest in ASP.NET 4.0

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

mocking Request.Files to test empty file upload

I am trying to unittest a file upload but seem to be missing something. The controller contains this fairly standard block in the httpPost handler: foreach (string file in Request.Files) { var postedFile = Request.Files[file] as HttpPostedFileBase; if (postedFile.ContentLength == 0) continue; var fileName = "~/Uplo...

Moq: How to get to a parameter passed to a method of a mocked service

Imagine this class public class Foo { private Handler _h; public Foo(Handler h) { _h = h; } public void Bar(int i) { _h.AsyncHandle(CalcOn(i)); } private SomeResponse CalcOn(int i) { ...; } Mo(q)cking Handler in a test of Foo, how would I be able to check what Bar(...

Mocking a type with an internal constructor using Moq

I'm trying to mock a class from the Microsoft Sync Framework. It only has an internal constructor. When I try the following: var fullEnumerationContextMock = new Mock<FullEnumerationContext>(); I get this error: System.NotSupportedException: Parent does not have a default constructor. The default constructor must be explicit...

MS Unit Testing in VS 2010, Test Method [my method] threw an exception: ..., WT*?

Hi, I've got an ASP.NET MVC 2 Web app in VS 2010 and decided to try using MS unit testing stuff. What do you know, on the first test I created it gives me grief and refuses to elaborate. I have created a single test class and a single test method. I am using Moq to create a HttpContext (including Request, Response, Session, QueryString...

How to prevent a call to the data layer from a mocked object

I have a business object that I am mocking (using Moq) for business tests. When I call the object, it calls another business object which goes to the data layer. I can set a return to calls within the object that I wrote, but how can I prevent it from calling the other object and going into the data layer? Is there a standard practice f...

Moq - Using VerifySet to check number of times called

I am trying to use VerifySet with Moq to check the number of times a setter on a collaborating Object is being called. But when I put in the Times portion of the call I get an error that the assignment operator is not valid in an expression tree. mockTimer.VerifySet(timer => timer.Prop = value); //Works fine mockTimer.VerifySet(timer =>...