moq

Moq - How to verify that a property value is set via the setter

Consider this class: public class Cotent { public virtual bool IsCheckedOut {get; private set;} public virtual void CheckOut() { IsCheckedOut = true; } public virtual void CheckIn() { //Do Nothing for now as demonstrating false positive test. } } The Checkin method is intentionally empty. Now i have ...

Moq Mocking and tracking Session values

I'm having problems returning a Session value set from mocking using Moq. Using the following public class TestHelpers { public long sessionValue = -1; public HttpContextBase FakeHttpContext() { var httpContext = new Mock<HttpContextBase>(); var session = new Mock<HttpSessionStateBase>(); httpContext.Setup(x => x.Session).Retu...

MOQ - Have a moq valid only once.

This is my problem test: [Test] public void PlayerHasPointsIncreasedOnEnterByFifteen() { // Arrange. var playerOneFake = new Mock<IEntity>(); playerOneFake.SetupGet(p => p.Score).Returns(0); var pointState = new PointState(playerOneFake.Object); // Act. pointState.Enter(); ...

Why do we need mocking frameworks?

I have worked with code which had NUnit test written. But, I have never worked with mocking frameworks. What are they? I understand dependency injection and how it helps to improve the testability. I mean all dependencies can be mocked while unit testing. But, then why do we need mocking frameworks? Can't we simply create mock objects an...

What are the real-world pros and cons of each of the major mocking frameworks?

see also "What should I consider when choosing a mocking framework for .Net" I'm trying to decide on a mocking framework to use on a .NET project I've recently embarked on. I'd like to speed my research on the different frameworks. I've recently read this blog post http://codevanced.net/post/Mocking-frameworks-comparison.aspx ...

How do I mock an IEnumerable<T> so that I can test a method that receives it

I have a method that I want to test which expects an IEnumerable<T> as a parameter. I'm currently mocking the contents of the IEnumerable<T> as follows (Using Moq): var mockParent = new Mock<ICsvTreeGridExportable>(); var mockChild = new Mock<ICsvTreeGridExportable>(); How do it put these mocked objects inside an IEnumerable<T> so ...

Moq and DataContext

I am new to Moq and need to know if I am doing this right. In AccountController.cs I have this: int id = _userRepository.GetProfileFromUserName(userName).ProfileID; UserRepository is mocked, but ProfileID comes from DataContext, so I did this in my AccountControllerTests.cs: mockUserReposository.Setup(gp => gp.GetProfile...

How to test method call order with Moq

At the moment I have: [Test] public void DrawDrawsAllScreensInTheReverseOrderOfTheStack() { // Arrange. var screenMockOne = new Mock<IScreen>(); var screenMockTwo = new Mock<IScreen>(); var screens = new List<IScreen>(); screens.Add(screenMockOne.Object); screens.Add(screenMockTwo....

Ramifications of Virtual Methods/Properties

Ola the 'flow! I have been using Moq recently in my development process and I like what I am able to achieve. However, I find myself making my methods (and properties for the mostpart) virtual so that I can replace them with a mock in my tests. Other than "you are making all your methods and properties overrideable", what real world r...

Moq Roles.AddUserToRole test

I am writing unit tests for a project in ASP.NET MVC 1.0 using Moq and MvcContrib TestHelper classes. I have run into a problem. When I come to Roles.AddUserToRole in my AccountController, I get a System.NotSupportedException. The Roles class is static and Moq cannot mock a static class. What can I do? ...

Moq Linq-to-SQL readonly property

I have a table aspnet_User in my model(dbml file) where I have a property UserName which is ReadOnly. I thought I could do this. var mockAsp_NetUser = new Mock<aspnet_User>(); mockAsp_NetUser.SetupGet(au => au.UserName).Returns("JohnDoe"); But then I get an exception: Invalid setup on a non-overridable member. An easy solution would ...

Can you verify a mock object if you don't have its Moq-wrapper?

Can anyone explain to me how to verify mocks if you don't have their Moq-wrapper? MockFactory.Verify() won't do. I want to be able to verify the mocks explicitly and mocks should be created using mockfactory! Please send in your comments. ...

Why did a Moq-mocked method return null?

Hi, I need help with a testmethod im trying to write... I need to test that a user can show his profile, however i encounter an error when i try to use my mocked GetProfileFromUserName method. The methods returns null. What I Dont understand is that i have a similar method named GetEmail, which basically does the same and works. This i...

how to mock container.Resolve<Type>()

i have a something like this public class HomeController { public ActionResult Index() { var x = Container.Resolve<IOrganisationService>(); } } when unit testing i get a null reference exception when the container tries to resolve anybody knows how do to mock Container.Resolve() ? ...

Moq setup confusion in C#

So I have the following class: public class MyClass { internal void A() { foreach(Thing thing in ThingArray) B(thing); } virtual internal void B(Thing thing) { // do some stuff } } And then I have the following test: ... var testObject = new Mock<MyClass>(parameters); testObject.Setup(t => t.B(It.IsAny<Thing...

How to find the value that has been passed to a method on my mocked (Moq or Rhino Mocks) interface?

I am using Moq - but could easily swap to another mock framework if needed. I have a interface defined: public interface IBaseEngineManagerImp { void SetClientCallbackSender(IClientCallbackSender clientCallbackSender); } I then mock IBaseEngineManagerImp with mockEngineManagerImp = new Mock<IEngineManagerImp>(); EngineManager eng...

How to verify that method argument's property values are set when mocking methods with Moq?

Not sure if it has been asked before, here is the question. Code first: public class Customer { public string Password { get; set; } public string PasswordHash { get; set; } } public class CustomerService { private ICustomerRepository _repo; public CustomerService(ICustomerRepository repo) { _repo...

Comparison between Unity and Moq Dependency Injection Frameworks

Faced with choosing a Dependency Injection Framework in a historically MS shop working with C#, I'm interested in finding out the differences between Moq and Unity. Some of the main concerns would include: Ease of use for developers with no background in DI Feature comparison between the two (once everyone becomes familiar with the te...

ASP.NET MVC - test the controller returning different views depending on action method logic

Hi, my controller can return different views depending on action method logic. Action method 'Create' asks service to do some validation and persistence. If validation fails, action method returns same 'Create' view. If validation and save runs OK, action method returns 'Index' view (RedirectToAction). I know that getting view name is po...

Moq'ing the raising of events multiple times

In a particular unit test I'm trying to raise an event multiple times and then to veryify a property value after the final event has been raised. I have something like public void TurnRight() { var mockFoo = new Mock<IFoo>(); SomeService someService= new SomeService (); someService.Foo= mockFoo.Object; mockFoo.Raise(foo=> ...