moq

Testing controller Action that uses User.Identity.Name

Hello, I have an action that relies on User.Identity.Name to get the username of the current user to get a list of his orders: public ActionResult XLineas() { ViewData["Filtre"] = _options.Filtre; ViewData["NomesPendents"] = _options.NomesPendents; return View(_repository.ObteLiniesPedido(User.Identity.Name,_...

Using Moq, how do I verify all properties of an object were copied?

I have an interface with a CopyFrom() method that copies all properties from another object. I have a test that performs several VerifyGet() calls to ensure that each property was retrieved from the passed object, e.g.: Thing target = new Thing(); IThing source = new Mock<IThing>(); target.CopyFrom(source.Object); source.VerifyGet(t =...

Unit testing using Moq, Code using Linq to Sql to stored procedure

I am very new to TDD, ASP.NET MVC and LinqToSql. I am trying to write tests for my repository, which gets data from stored procedure using LinqToSql. This is the repository method being tested: public int GetResponseCount(int campaignId) { var results = (new MyDBDataContext()).GetResponseCountById(campaignId); foreach (GetRespo...

How to Verify another method in the class was called using Moq

This seems like something simple but I can't seem to get it to work. I have a class with a Save method that simply calls another method ShouldBeCalled(). I want to verify that if I call Save() that the other method ShouldBeCalled() is executed at least once. I thought that I could do the following. public class ClassA { public vir...

How do I unit test a method that is expecting an object to be updated via a reference?

I'm having trouble unit testing a method that changes some properties of a reference type that is passed to it. As an example, let's say I have a class called Policy. Policy policy = new Policy(); policy.Status = Active; I then pass this policy to the policy manager in order to inactivate the policy. policyManager.InactivatePolicy(p...

Moq, VB, HttpResponseBase and Headers

I'm in the process of writing a heap of tests around some custom controllers using Moq in VB. Up until now, I've not had to deal with VB Lambda shortcomings since I've only moqed properties or methods. That is until this morning when I try also running integration tests using Cassini against my code. I had code to add headers using Resp...

Verifying event registration using Moq.net

I'm developing an asp.net (classic) application trying to implement the MVP pattern using this example. In trying to unit test my presenter and using the following pattern, the psuedocode for which looks like so //base view interface public interface IView { event EventHandler Init; event EventHandler Load; bool IsPostBack...

List of Mock (Moq) objects - best practices/simplification.

Consider the following: new SUT(null, null, new List<IObjectBeingMocked>() { mockObjectOne.Object, mockObjectTwo.Object }) My SUT (System Under Test) needs a list of objects as the third parameter. These need to be mocks as I've set some expectatioins on these. How would I clear it up so that I can remove the need to call .Object on ...

What is needed in the HttpContext to allow FormsAuthentication.SignOut() to execute?

I am trying to write a unit test for our log out method. Amoung other things it FormsAuthentication.SignOut(). However, it throws a System.NullReferenceException. I've created myself a mock HttpContext (using Moq) but it is obviously missing something. My mock context contains: A mocked HttpRequestBase on Request A mocked HttpRespo...

Moq: Specifying return values as part of expectations

Hi, I'm new to Moq and learning. I need to test that a method returns the value expected. I have put together a noddy example to explain my problem. This fails miserably with: "ArgumentException: Expression is not a method invocation: c => (c.DoSomething("Jo", "Blog", 1) = "OK")" Can you correct what I am doing wrong? [TestFixtu...

moq - how to verify method has not been called if the class swallows exceptions

I am trying to test a fairly complex class using Moq and am running into a problem. I am trying to verify that a method does NOT get called, and usually this is simple to do by setting MockBehavior.Strict, but here however the class has its own error reporting mechanism so it swallows the exception being thrown by Moq. .VerifyAll metho...

How do I mock the HttpContext in ASP.NET MVC using MOQ?

[TestMethod] public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist() { var context = new Mock<HttpContextBase>(); var request = new Mock<HttpRequestBase>(); context .Setup(c => c.Request) .Returns(request.Object); HomeController controller = new HomeC...

How to trigger Initialize method while trying to Unit Test?

I have the override below to do a few things with cookies using the FormsIdentity Object. But when I instantiate a controller on my Unit Testing this method is not triggered. Any ideas how to do this? protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); ...

How to Bypass HTTPContext Usage .?

var mockedService = new Mock(); mockedService.Setup(x => x.InterfaceMethod(args)).Returns(value); _Service = mockedService.Object; MyController controller =new MyController(_Service); var result = (ViewResult)controller.Foo(); Now this Foo() Method contains the Following API Call HttpContext.GetGlobalResourceObject(...,...

Mocking virtual readonly properties with moq.

I couldn't find a way to do this, though this can be done by hand so why not with moq? ...

Moq how do you test internal methods?

Hi, Told by my boss to use Moq and that is it. I like it but it seems that unlike MSTest or mbunit etc... you cannot test internal methods So I am forced to make public some internal implementation in my interface so that i can test it. Am I missing something? Can you test internal methods using Moq? Thanks a lot ...

Moq - Ordered Setups (Expectations)

Hi All, I am fairly new to Moq and cant work out how I am to order setups. I have the following code: _orderRepository.Setup(r => r.Update(It.Is<Order>(a => ((int)a.OrderStatusReference.EntityKey.EntityKeyValues[0].Value) == 2))) .Throws(exception) .AtMost(5); I want this to be executed...

How do I setup a call of an Equals with a specific type overriding the Equals in MoQ?

Working with the fine mocking-framework MoQ, I came across a somewhat surprising facet (and I don't like surprises). I'm mocking out a class which should be added to a collection after a method-call, like so: public class SomeClass{ } public class Container { private List<SomeClass> classes = new List<SomeClass>(); public IEnumerab...

How can I emit a .NET type with two properties that are overloaded only on return type?

I need to create a type that has two properties with the same name, and only differ on return type. Dynamically emitting this type via reflection is perfectly acceptable. Something like this: public TypeA Prop { get; } public TypeB Prop { get; } I do understand that I cannot consume this property from C# or VB.NET or lots of other .N...

How to mock up a static method in a static class with URLHelp? (Moq)

I have a extension method. Can any one help me how to test this method with Moq? public static string GetBaseUrl(this UrlHelper urlHelper) { Uri contextUri = new Uri(urlHelper.RequestContext.HttpContext.Request.Url, urlHelper.RequestContext.HttpContext.Request.RawUrl); UriBuilder realmUri = new UriBuilder(contextUri...