moq

Testing a method called many times in moq

I have an interface like so: Interface IWriteFile { string FileName {get;set;} void Open(); void WriteData(string dataToWrite); void Close(); } I want to test a class that will use this interface to populate a file. It will be calling WriteData a bunch of times and I just want to test the final output. Is there a way to intr...

Mocking Prism Event Aggregator using Moq for Unit Testing

Hi all, I need some advice on how to use Moq in a unit test to make sure that my class under test is behaving how I want. That is the class under test publishes an Event Aggregator (from Prism) event and I need some way of asserting that this event has been raised in my test. I don't have a lot of resource at work and am finding it dif...

Is it possible to return a mock from another mock using Moq in C#?

Hi, I am using Moq as my mocking framework. As per the code below, I have two mocks setup and I would like to setup the second to return the first mock. Is this possible and if so how would I go about doing it? At the moment it says the mock being returned is an invalid candidate. [SetUp] private void SetupMarketRow() { var marketTot...

MissingMethodException thrown when trying to mock HtmlHelper with Moq

When attempting to follow the article on mocking the htmlhelper with Moq I ran in to the following problem. The exception is thrown on creation of the htmlhelper. I am only guessing that castle windsor is being used (by seeing the error message). The exception: MissingMethodException occurred Constructor on type 'Castle.Prox...

Verify method was called with certain linq expression (moq)

Can't figure out the syntax. //class under test public class CustomerRepository : ICustomerRepository{ public Customer Single(Expression<Func<Customer, bool>> query){ //call underlying repository } } //test var mock = new Mock<ICustomerRepository>(); mock.Object.Single(x=>x.Id == 1); //now need to verify that it was called ...

Having trouble getting started with Moq and Nunit

Hi all, Banging my head against a wall trying to get a really simple testing scenario working. I'm sure I'm missing something really simple! Whatever I do, I seem to get the following error from the NUnit gui when running a test against my DLL: System.TypeLoadException : Type 'Castle.Proxies.ITestProxy' from assembly 'DynamicProxyGenAs...

Mocking a session wrapper in MVC2

I've seen how to Fake the SessionState object in MVC using Scott Hanselmans MvcMockHelpers, but I'm dealing with a separate problem. What I like to do is create a wrapper around the Session object to make objects a little more accessible and strongly typed rather than using keys all over. Here is basically what it does: public class S...

Why doesn't the following mocking with Ninject.Moq work?

I'm trying to run the following code with Ninject.Moq: [TestMethod] public void TestMethod1() { var kernel = new MockingKernel(); var engine = kernel.Get<ABC>(); //as I don't need to actually use the interfaces, I don't want //to even have to bother about them. Assert.AreEqual<string>("abc", engine.ToString()); } A...

MVC Mock HttpContextBase which is used in a helper

I am using a helper in my controllers and in my views that I have found somewhere on the internet. The helper is called like this in my controller "Url.SiteRoot();" How can I get my controller to not throw an Exception whenever the helper is called? I am using MVCContrib and moq for my unit tests. I am thinking of implementing some kind...

MOQ - how to mock an interface that needs to be cast to another interface?

what I want to do is construct a moq for I1 - which is fine ... however in the course of the method that I am testing that uses this mock I need to cast it to I2 in order to access some properties that are not on I1 Interface I1 { int AProperty{get;set;}} Interface I2 {int AnotherProperty{get;set;}} I then have some objects Class O...

C# Unit Test error, could not load file or assembly when running test

I'm working on a unit test in a project using the MOQ-framework, C# 4.0, MVC2.0 The test is similar to the one below. But as soon as i run this test i'm getting a strange error. I've checked all the referenced System.Web.mvc assemblies and there all at version 2.0.0, so it seems to me that those can't cause the issue. [TestMethod] ...

Custom Matching with Moq

I am attempting to be a good TDD citizen as I design an application. I'm using Moq, and I've run into a little repository issue. My repository has a Find method: public IEnumerable<T> Find(Expression<Func<T, bool>> where) { return _objectSet.Where(where); } Then I attempt to set up a mock of the repositor...

Using Moq to override virtual methods in the same class

We are using Moq to unit test our service classes, but are stuck on how to test situations where a service method calls another service method of the same class. I tried setting the method being called to virtual, but still couldn't figure out what to do then in Moq. For example: public class RenewalService : IRenewalService { //w...

Moq modify protected on calling void method

I want to use moq a void method and set a value to a protected property when is called. public class MyClass{ public Guid Id {get; protected set; } } public interface IMyRespository { public void Save(MyClass myClass); } Something like: var moq = new Mock<IMyRespository>(); var my = new MyClass(); moq.Setup(x=>x.Save(my)); I want...

Using IoC container as a service locator for HttpHandler

This question relates to my other post. Ok so after a bit more messing around I decided to do it this way. Which seems to work fine when I run it, although I'm getting the following error in NUnit: Could not load file or assembly 'Castle.Core, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies....

Mocking, Unit Testing (NUnit) setup problem with HttpHandler

This post relates to two other posts, here and here. I'm new to Unit Testing & Mocking. I have a test fixture that is trying to mock a HttpContext object including the response and request. I think the test code is not setup properly, as after calling the handler I get an error immediately. There error I am getting is: UnitTests.UADHan...

Moq : How to mock a class which is not visible?

I've the following simplified code which describes my problem: public interface IMyUser { int Id { get; set; } string Name { get; set; } } Which is used in the dataccess layer like this: public interface IData { T GetUserById<T>(int id) where T : IMyUser, new(); } The userlogic class is defined as follows: public class...

Moq: How to mock a generic method (when the Generic Type T is internal) ?

I've the following method defined public interface IData { T GetUserById<T>(int id) where T : IMyUser, new(); } The actual use of this method is like : da.GetUserById<MyUser>(id); where the MyUser is an internal class defined in the business logic and cannot be used by the unittest. In NMock this can be done using this code: ...

How to test a method in an abstract class with abstract methods?

It is necessary to check implementation of 'MyMethod' virtual method in the abstract 'MyAbstractClass': public abstract MyAbstractClass { public void MyMethod() { Testpassed = true; } public abstract int StatusCode{get;internal set;} // **EDIT**: internal setter was added public bool TestPassed{get;private ...

ASP.NET MVC - Unit testing RenderPartialViewToString() with Moq framework?

I'm using this helper method to turn my PartialViewResult into string and returning it as Json - http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/ My problem is that I'm using Moq to mock the controller, and whenever I run unit test that uses this RenderPartialViewToString() method, I got the "Object reference...