moq

How to mock HttpClientCertificate?

I am trying to unit test an action filter I wrote. I want to mock the HttpClientCertificate but when I use MOQ I get exception. HttpClientCertificate doesnt have a public default constructor. code: //Stub HttpClientCertificate </br> var certMock = new Mock<HttpClientCertificate>(); HttpClientCertificate clientCertificate = certMock.Obj...

Mocking ref parameters

I've cross posted this on the #moq discussion group at: http://groups.google.com/group/moqdisc/browse_thread/thread/569b75fd2cc1829d hey folks, I have come across a problem with a mocked ref param that I'm sure must be obvious, but being new to the framework I just can't work it out. I have the following repository method: public int...

Why is mocking preferred with Interfaces?

I have been looking at examples of mocking using Moq and Rhino Mocks and all the examples seem to mock interfaces. Why is this? I have heard they can mock static classes, but what about non-static classes? ...

Mocking methods with Expression<Func<T,bool>> parameter using Moq

I want to mock this interface using Moq IInterfaceToBeMocked { IEnumerable<Dude> SearchDudeByFilter(Expression<Func<Dude,bool>> filter); } I was thinking of doing something like _mock.Setup(method => method.SearchDudeByFilter( x=> x.DudeId.Equals(10) && X.Ride.Equals("Harley"))). Returns(_dudes);// _dudes is an in-memory list of dud...

Moq and constructors - testing initialisation behaviour

I'd like to be able to test a class initialises correctly using Moq: class ClassToTest { public ClassToTest() { Method1(@"C:\myfile.dat") } public virtual void Method1(string filename) { // mock this method File.Create(filename); } } I thought I'd be able to use the CallBase property to...

Unit Testing Mock/Stub definitions in Moq

Any reading or advice I've been given on Unit Testing has always suggested a distinct difference between the definition of a Mock and a Stub. My current understanding of these definitions are as follows Mock: A fake which will be used in your test to make a final assertion Stub: A fake which will be used in your test to isol...

Moq Event Aggregator Is it possible

Hi All Wondering if its possible to Moq the Prism EventAggregator Let's take the EventAggregator Quickstart they have [TestMethod] public void PresenterPublishesFundAddedOnViewAddClick() { var view = new MockAddFundView(); var EventAggregator = new MockEventAggregator(); var mockFundAddedEven...

How to list all methods-of-a-class/classes that are not tested?

I want to have a list of not-tested methods/classes but not sure how to get it. Please help. [Edit] I'm using test framework Moq. ...

Syntax Comparison between Moq and Rhino mocks

My company is trying to decide if we are going to standardize on Moq, Rhino Mocks or MS Moles and Stubs. I know Rhino Mocks and Moles and Stubs fairly well. But I am unfamiliar with Moq. How does the syntax work? Does it support Arrange Act Assert (AAA) like Rhino Mocks (I hear they created it, but I am not sure). Does it have stron...

What is the differences between "Strict" and "Loose" behavior?

The short document of Moq doesn't help me to understand this. Help! ...

What is the purpose of VerifyAll() in Moq?

I read the question at http://stackoverflow.com/questions/980554/what-is-the-purpose-of-verifiable-in-moq and have this question in my mind. Need your help to explain that. ...

How can I mock an OracleConnection and OracleCommand?

For my tests I need to mock out the data client, in my case they are Oracle. I have created my data access layer to allow this to be passed in: public static Int32? GetUserRoleId(string userName, OracleConnection cn, OracleCommand cmd) I am using Moq, though I can switch to another framework if needed, and when I go to create the Moc...

Verify the number of times a protected method is called using Moq

In my unit-tests I'm mocking a protected method using Moq, and would like to assert that it is called a certain number of times. This question describes something similar for an earlier version of Moq: //expect that ChildMethod1() will be called once. (it's protected) testBaseMock.Protected().Expect("ChildMethod1") .AtMostOnce() .Ve...

How to test lambda functions with Moq?

There is a function: public class MyCacheClass : ICache { public void T GetObject<T>(Func<T> func) { T t; ... t = func(); ... return t; } } public class MyWorkClass : IWork { public Object MyWorkMethod(string value) { return new object(); } } These functions are ...

Can MOQ Mock a Class

is it legal to MOQ a class that does not inherit an interface like so: var mockIActionService = new Mock<IActionService>(); var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object); I inject the IService into ValidAgeRule which is just a simple class with one method called "Excute". My question is how to I verify that ...

MOQ Testing return types

Given the following: var mockIActionService = new Mock<IActionService>(); var mockValidAgeRule = new Mock<ValidAgeRule>(mockIActionService.Object); mockValidAgeRule.Setup(t => t.Execute(app)); Now t.Execute returns a "Rules" object, how can I verify that something has been called on Rules? I am attempting to call it as such mockVali...

Use DataTemplate in WPF with a mocked object.

I have following xaml code: <Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" DataContext="{Binding MainWindow, Source={StaticResource Locator}}"> <Window.Resources> <DataTemplate DataType="{x:Type vm:...

Moq SetupGet errors but setting property on .Object works fine

I'm trying to define a return value for a Mock (model). Can anyone explain to me why the following code fails with a "Invalid setup on a non-overridable member": model.SetupGet(x => x.PostCodeSearch).Returns(It.IsAny<string>); and yet I can do this and it works fine: model.Object.PostCodeSearch = "Any value as long as it's not null ...

How do I unit test protected properties meant to be set only by NHibernate?

I'm using NHibernate to persist this entity: public class Store { public int Id { get; protected set; } public int Name { get; set; } } Note how the Id property has a protected setter. This is to prevent users from changing the Id while still allowing NHibernate to assign it an Id when it saves it to the database. In one of m...

How to Mock (with Moq) Unity methods

Extension methods are not good for testing (that's described here: http://stackoverflow.com/questions/2295960/mocking-extension-methods-with-moq, http://www.clariusconsulting.net/blogs/kzu/archive/2009/12/22/Howtomockextensionmethods.aspx). But probably there are some solutions for mocking of Unity methods? In my case I have the followi...