moq

Rhino mock vs Typemock vs JustMock vs...

I need to choose mock framework to new project. What are the pros and cons for those frameworks? Any comparison table? I know that JustMock is i beta stage but it's look very good right now (very similar to TypeMock) Edit: I'v What about MS Mole? ...

Moq, a translator and an expression

I'm working with an expression within a moq-ed "Get Service" and ran into a rather annoying issue. In order to get this test to run correctly and the get service to return what it should, there's a translator in between that takes what you've asked for, sends it off and gets what you -really- want. So, thinking this was easy I attempt ...

How to mock an SqlDataReader using Moq - Update

Hi, I'm new to moq and setting up mocks so i could do with a little help. Title says it all really - how do I mock up an SqlDataReader using Moq? Thanks Update After further testing this is what I have so far: private IDataReader MockIDataReader() { var moq = new Mock<IDataReader>(); moq.Setup( x => x.Read() ).Returns( true...

Why in the world is this Moq + NUnit test failing?

I have this dataAccess mock object and I'm trying to verify that one of its methods is being invoked, and that the argument passed into this method fulfills certain constraints. As best I can tell, this method is indeed being invoked, and with the constraints fulfilled. This line of the test throws a MockException: data.Verify(d => d....

Method for unit testing an extension method for SqlCommand

Hi, I've created an extension method for SqlCommand that allows some additional work before executing the command: public static SqlDataReader ExecuteMyReader( this SqlCommand sqlCommand ) { // Some calculations here return sqlCommand.ExecuteReader(); } My question is what is the best way to unit test this extension method? ...

Mock a Linq to Sql EntityRef using Moq?

My datacontext contains a table named Userlog with a one to many relationship with table UserProfile. In class UserLog public UserProfile UserProfile { get {return this._UserProfile.Entity;} } In class UserProfile public EntitySet<UserLog> UserLogs { get{return this._UserLogs;} } { set {this._UserLogs.Assign(value); } How wo...

Weird .net 4.0 exception when running unit tests

Hi guys I am receiving the following exception when trying to run my unit tests using .net 4.0 under VS2010 with moq 3.1. Attempt by security transparent method 'SPPD.Backend.DataAccess.Test.Specs_for_Core.When_using_base.Can_create_mapper()' to access security critical method 'Microsoft.VisualStudio.TestTools.UnitTesting.Asse...

How to mock ISerializable classes with Moq?

Hi there, I'm completly new to Moq and now trying to create a mock for System.Reflection.Assembly class. I'm using this code: var mockAssembly = new Mock<Assembly>(); mockAssembly.Setup( x => x.GetTypes() ).Returns( new Type[] { typeof( Type1 ), typeof( Type2 ) } ); But when I run tests I get next exception: System.A...

How to Moq Subscribe/Publish events of the aggregator

I am facing the following issue and appreciate your help for I have run out of ideas as how to fix. I have the following in my test: Mock> mockToolbarClickEvent3 = new Mock>(); _aggregator.Setup(e => e.GetEvent>()).Returns(mockToolbarClickEvent3.Object); In my code (that I am testing) I have the following for Publishing an event: Som...

Test MVC using moq

I am new to moq and I was trying to test a controller (MVC) behaviour that when the view raises a certain event, controller calls a certain function on model, here are the classes - public class Model { public void CalculateAverage() { ... } ... } public class View { public event EventHandler CalculateAvera...

Mocking objects with complex Lambda Expressions as parameters

I´m encountering this problem trying to mock some objects that receive complex lambda expressions in my projects. Mostly with with proxy objects that receive this type of delegate: Func<Tobj, Fun<TParam1, TParam2, TResult>> I have tried to use Moq as well as RhinoMocks to acomplish mocking those types of objects, however both fail. ...

Simple Moq ? - Problem with .Verify

Hi, just a simple question here. I've used Moq for awhile but, as of yet, have only used it for stubbing and not for mocking. I am trying to introduce our developers to unit testing. I set up a simple example to explain the concepts but i can't seem to get it working. Probably something simple so i thought i would just ask you all to see...

Unit testing with Mocks when SUT is leveraging Task Parallel Libaray

I am trying to unit test / verify that a method is being called on a dependency, by the system under test (SUT). The depenedency is IFoo. The dependent class is IBar. IBar is implemented as Bar. Bar will call Start() on IFoo in a new (System.Threading.Tasks.)Task, when Start() is called on Bar instance. Unit Test (Moq): [Test] ...

Moq.Mock<T> - how to setup a method that takes an expression

I am Mocking my repository interface and am not sure how to setup a method that takes an expression and returns an object? I am using Moq and NUnit Interface: public interface IReadOnlyRepository : IDisposable { IQueryable<T> All<T>() where T : class; T Single<T>(Expression<Func<T, bool>> expression) where T : class; } Test w...

How to mock the Request.ServerVariables using MOQ for ASP.NET MVC?

hi guys, i'm just learning to put in unit testing for my asp.net mvc when i came to learn about the mock and the different frameworks there is out there now. after checking SO, i found that MOQ seems to be the easiest to pick up. as of now i'm stuck trying to mock the Request.ServerVariables, as after reading this post, i've learned th...

Is there any good ASP.Net MVC project with TDD & MOQ Source code available?

hi guys, i'm starting to learn TDD, Unit-testing on asp.net mvc and i'm trying to pickup all these mocking via MOQ. so i'm looking for any good asp.net mvc projects which source codes are made available to mere mortals like me :) i've found some good asp.net mvc source codes but not those that uses MOQ specifically. the asp.net mvc s...

How to Mock the Internal Method of a class ?

I have a class which has a internal method and i want to mock the internal method . But i am unable to mock it i.e. it is not calling the mocked function but calling the original function. Is there any way to achieve this ? Edit:Actually i am a novice to the Moq. I have many classes and methods of the classes to test using the Moq. Man...

Moq: Unable to cast

Hello, I have the following mocks: var MockHttpContext = new Mock<HttpContextBase>(); var MockPrincipal = new Mock<IPrincipal>(); MockHttpContext.SetupGet(h => h.User).Returns(MockPrincipal.Object); The error occurs when testing this line: var user = (CustomPrincipal)httpContext.User; This is the error: Unable to cast object of ...

Moq: Unable to cast to interface

Hello, earlier today I asked this question. So since moq creates it's own class from an interface I wasn't able to cast it to a different class. So it got me wondering what if I created a ICustomPrincipal and tried to cast to that. This is how my mocks look: var MockHttpContext = new Mock<HttpContextBase>(); var MockPrincipal = new ...

Is this a good way to setup Moq to return a particular value only a certain number of times?

Using NUnit and Moq, I am trying to wrap some tests around legacy code before I do too much more refactoring. I need to test that a method keeps looping until told to stop. Here is my idea how to do it: [TestCase] public void KeepsCallingDoSomethingUntilShouldIKeepGoingIsFalse() { var dal = new Mock<IDataAccessLayer>(); var sut = ...