moq

Moq converts It.IsAny<Exception> to It.IsAny<string> in expectation

I'm using Moq for unit tests, and I've set up an expectation like this: myMock.Expect(w => w.MyMethod(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<System.Exception>(), null)) .Returns(myResult); the method it is mock...

Setting properties on a mocked HttpContextBase

I'm working on an ASP.NET MVC application, and am trying to write some unit tests against controller actions, some of which manipulate properties on the HttpContext, such as Session, Request.Cookies, Response.Cookies, etc. I'm having some trouble figuring out how to "Arrange, Act, Assert"...I can see Arrange and Assert...but I'm having t...

Mocking HttpContextBase with Moq

I have a unit test fixture in which I'm trying to test a ControllerAction on an ASP.NET MVC controller that's used for membership functions on a web app. I'm trying to mock the HttpContext for the tests. The ControllerAction under test actually sets properties on the HttpContext, such as Session values, Response.Cookies values, etc. This...

Moq and virtual properties and methods

I'm using Moq for unit testing. It order for Moq to work, properties and methodes have to be marked as virtual. Sometimes I pass in data and set property values in the constructors. Isn't there a rule that you should not set virtual properties in constrcutors since it might cause unexpected behaviour (if the class has been inherited from...

Moq with WinForms MVP Pattern - Failing Test

Hi all - I'm learning TDD and the MVP pattern. I've created a simple WinForms app that's like a replacement for the TOAD SQL tool. I'm trying to go back and write tests now for the code I've already written (which I know isn't the correct process for TDD, but please bear with me). In my test class for the form, I want to test the conc...

MOQ problem - mocked class returns incorrect data

So, I'm using moq for testing, but I ran into a problem that prevents me from mocking correctly, at least I think so. This is my repository class: public interface IAccountsRepository { IQueryable<Account> Accounts { get; } IQueryable<Account> AccountsPaged(int pageSize, int selectedPage); } This is one of the implem...

How do Moq and NHibernate automatically create derived types?

When using NHibernate, you define your entites with virtual methods, and NHibernate will create a proxy object that tracks changes to your object. In Moq, the framework will magically create a derived type from an interface or a base class. e.g. var mock = new Mock<IFoo>(); IFoo magicFoo = mock.Object; This is really cool. How do...

Can any one suggest a step by step example for using moQ framework

Can any one suggest a step by step example for using moQ framework. any guidelines or thumbrules that has to be followed while mocking objetcs . can be much help. thanks. ...

NUnit+Moq Throwing Exception

The code under test follows. view.QueryResultsGrid is a System.Windows.Forms.DataGridView object: public void SelectCheckedChanged(object sender, EventArgs e) { view.QueryResultsGrid.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect; } Test being attempted: private Mock<IQueryForm> mockWind...

Moq Match and Verify Array / IEnumerable parameters in method setup

I'm having problems verifying Ienumerable / Array type parameters when setting up expectation for methods call on my mock objects. I think since it's matching different references it doesn't consider it a match. I just want it to match the contents of the array, sometimes I don't even care about the order. mockDataWriter.Setup(m => m....

Question regarding setting up Moq behaviour using Setup().

I am trying out moq and I have a question regarding to the Setup() method. I have the following interface and class: public interface IMyInterface { void Print(string name); } public class MyClass { private IMyInterface my; public MyClass(IMyInterface my) { this.my = my; } public void Print() { ...

Mocking a base class method call with Moq

I am modifiying a class method which formats some input paramater dates which are subsequently used as params in a method call into the base class (which lives in another assembly). I want to verify that the dates i pass in to my method are in the correct format when they are passed to the base class method so i would like to Moq the ba...

Is there a way to copy a Moq Mock<> by value?

I am using an interface (IDataSet) in front of System.Data.DataSet for testing purposes. I want to be able to mock the Copy() method. Basically I want a copy/clone of the same mocked object. Here's some pseudo code of what I would like to do. Mock<IDataSet> dataMock = new Mock<IDataSet>(); Mock<IDataSet> copyMock = ??? // How do I...

How to setup IPrincipal for a mockup?

Hi I want to mockup IPrincipal so I did this public Mock<IPrincipal> Principal { get; set; } in my setup of my nunit Principal = new Mock<IPrincipal>(); So this should be all that I need in my nunit unit test but how about in my actual controller file? Like how do I set it up? For example I have a membership.Provider So what I...

Moq Query Never Returning

I'm not sure what i'm doing wrong, but this line of code is never returning (seems to result in a run-away process) when running it in a test: var discountMemberCustomer = (from customer in Mocks.Query<Customer>() where customer.IsDiscountMember && customer.OrderCount == 13 && customer.LifetimeCustomerValue == 5555m ...

Is Mocking able to replace functionality wrapped inside a method?

I'm trying to define a way to simulate an case on accessing into a database without accessing... That's probably sounds quite crazy, but it's not. Here is an example about a method i would like to test: public IDevice GetDeviceFromRepository(string name) { IDevice device = null; IDbConnection connection = new Sq...

How to unit test TimeZones?

Hi I am wondering how to you test TimeZone logic? For me my timezone stuff is used to filter data from the database(what of course in unit tests gets mocked up and not tested). But I would like to test the logic around it to see if I give certain times in that it would be converted right. I am not sure how to do this though. ...

Setting up a Moq object question.

Hi I am using constructor injection pattern to insert my mocks with moq. So I have something like this in my nunit test property UserMock property IService // interface public void PreSetup() { UserMock = new UserMock; ITaskService = new Service(UserMock.object); } now I have a unit test method like this public void TestSo...

ASP.NET MVC Unit Testing - Sessions

Having searched StackOverflow, and Google I think what I'm doing is suppose to be right, however results don't seem to be going well [TestMethod] public void LoginAction_Should_Return_View_and_User_Authenticated() { // Arrange var mock = new Mock<ControllerContext>(); var mockSession = new Mock<HttpSe...

How to throw a SqlException(need for mocking)

Hi I am trying to test some exceptions in my project and one of the Exceptions I catch is SQlException. Now It seems that you can't go new SqlException() so I am not sure how I can throw a exception especially without somehow calling the database(and since it is unit tests it is usually advised not to call the database since it is slow...