moq

Rhino Mocks, TypeMock, Moq, or NMock? Which one do you use and why?

Which one do you use (if you use the listed ones) and what do you love and even hate about it? ...

Moq'ing an interface

While I'm googling/reading for this answer I thought I would also ask here. I have a class that is a wrapper for a SDK. The class accepts an ILoader object and uses the ILoader object to create an ISBAObject which is cast into an ISmallBusinessInstance object. I am simply trying to mock this behavior using Moq. [TestMethod] p...

Mocking an attribute change on a parameter - using Moq

I am using Moq to mock my Repository layer so I can unit test. My repository layer Insert methods update the Id property of my entities when a successful db insert occurs. How do I configure moq to update the Id property of the entity when the Insert method is called? Repository code:- void IAccountRepository.InsertAccount(AccountE...

Where is the MOQ documentation?

Where can I find comprehensive documentation for MOQ? I'm just starting with mocking and am having difficulty getting my head around it. I've read through all the links at http://code.google.com/p/moq/wiki/QuickStart but can't seem to find a tutorial or gentle introduction. I have also looked briefly at Rhino Mocks but found it very con...

moq - good sample apps

Hi guys I know that there has been a couple questions about tutorials on moq. But I am wondering if there are any sample apps out there that use moq in the context of an n-tier business application using ado.net. I find the tutes good, but they don't seem to bring everything all together into the big picture. Thus, I am looking for a s...

Moq Expect On IRepository Passing Expression

I am using this code to verify a behavior of a method I am testing: _repository.Expect(f => f.FindAll(t => t.STATUS_CD == "A")) .Returns(new List<JSOFile>()) .AtMostOnce() .Verifiable(); _repository is defined as: private Mock<IRepository<JSOFile>> _repository; When my test is run, I get this exception: Expression ...

Mocking a COM object

I have been working on a wrapper for a COM object that can only take and return strings. The interface for the COM object looks like this: interface IMapinfo { void Do(string cmd); string Eval(string cmd); } Now I have made classes that wrap up basic functions like so: public class Table { ...

Moq multi-interface question

This may or may not be a multi-interface problem, but I'm doing something like this: var mockInterface1 = new Mock<IInterface1>(); var mockInterface2 = mockInterface1.As<IInterface2>(); mockInterface1.Expect( foo => foo.Foo(It.IsAny<IInterface3>() ) ); ... otherObject.DoSomething( (IInterface1)mockInterface2.Object ); On the DoSomet...

How can I use Mock Objects in my unit tests and still use Code Coverage?

Presently I'm starting to introduce the concept of Mock objects into my Unit Tests. In particular I'm using the Moq framework. However, one of the things I've noticed is that suddenly the classes I'm testing using this framework are showing code coverage of 0%. Now I understand that since I'm just mocking the class, its not running the ...

How to MOQ an Indexed property

I am attempting to mock a call to an indexed property. I.e. I would like to moq the following: object result = myDictionaryCollection["SomeKeyValue"]; and also the setter value myDictionaryCollection["SomeKeyValue"] = myNewValue; I am doing this because I need to mock the functionality of a class my app uses. Does anyone know how...

Using Moq to determine if a method is called

Im very new top mocking frameworks, but I have decided to take a look at Moq. It is my understanding that I can test that a method call will occur if I call a higher level method i.e. public abstract class SomeClass() { public void SomeMehod() { SomeOtherMethod(); } internal abstract void SomeOtherMethod(...

What is the difference between Moq-ing a class or interface?

I've been using moq to mock objects in my unit tests and I've seen on the site about moq that it is able to mock both classes and interfaces. I had a discussion with one of my work mates the other day and they stated that there is never a reason to mock classes and I should only mock interfaces. I didn't really have an answer to that.....

Moq tests using ExpectSet() with It.Is<T>() aren't behaving as... expected.

I've isolated the behaviour into the following test case. I'd be grateful to anyone who can tell me how to expect/verify a property set for a List<T> property - it appears there's something going on inside It.Is<T>(predicate) that isn't making a whole lot of sense to me right now. Sample code will run as a console app from VS2008 - you'l...

Verifying protected abstract methods are called using Moq

Suppose I have the following class: public class TestBase { public bool runMethod1 { get; set; } public void BaseMethod() { if (runMethod1) ChildMethod1(); else ChildMethod2(); } protected abstract void ChildMethod1(); protected abstract void ChildMethod2(); } I also have the class public class Chi...

Having issues with moq

I am trying out moq and I'm running into a problem with the following test body: var child = new Mock<ZooNode>(); var parent = new Mock<ZooNode>(); child.Object.Parent = parent.Object; parent.Expect(p => p.Children.Contains(child.Object)).Returns(true); which throws : System.ArgumentException: Invalid expectation on a non-overr...

Why do Rhino.Mocks and Moq say that Bar is a non-overridable member?

Could someone explain why both tests using the latest versions of Moq and Rhino.Mocks frameworks fail complaining that Bar is not a virtual/overridable method: public interface IFoo { string Bar(); } public class Foo : IFoo { public string Bar() { return "Bar"; } } [TestMethod] public void MoqTest() { var f...

Mock set indexer in Moq

I can do: var req = new Mock<HttpRequestBase>(); var vals = new Dictionary<string, string>(); req.Expect(r => r[It.IsAny<string>()]) .Returns(s => vals[s]); But what about something like this ??? req.ExpectSet(r => r[It.IsAny<string>()], It.IsAny<string>()) .Callback((s, v) => vals[s] = v); ...

Why does this NUnit+Moq test fail?

I hope you know, because I don't see it. Bonus points for whoever figures out how one can achieve what this test is trying to achieve. using NUnit.Framework; using Moq; [TestFixture] public class MoqHuh { public class A {} public class B : A {} public interface IHelper { void DoIt(A a); } [Test] pu...

Moq: how to assert that methods on my mock object are NOT run?

I have mocks working where I test that methods on my mocked object are called with the correct parameters, and return the right result. Now I want to check another condition. In this case, NO methods should be run on the mocked object. How can I express this in a unit test? ...

Why does my object mocking fail?

I am using the Moq and can't seem to get my unit test to pass on what appears to be a simple mocking scenario. Product p = new Product(); var rep = new Mock<IProductRepository>(); rep.Expect(x => x.GetProductById(1)).Returns(p); p = rep.Object.GetProductById(1); Assert.AreEqual(1, p.ProductId); //Assert.AreEqual failed. Expected:<1>....