I have a switch statement in a factory that returns a command based on the value of the enum passed in. Something like:
public ICommand Create(EnumType enumType)
{
switch (enumType)
{
case(enumType.Val1):
return new SomeCommand();
case(enumType.Val2):
return new SomeCommand();
case(enumType.Val3...
Hello,
I am starting a new project with NHibernate, ASP.NET MVC 2.0 and StructureMap and using NUnit and Moq for testing. For each of my controllers I have a single public constructor into which an ISession is being injected. The application itself works just fine, but in terms of unit testing I essentially have to mock an ISession in or...
How can i test that a particular method was called with the right parameters as a result of a test? I am using nunit.
The method doesn't return anything. it just writes on a file. I am using a moq object for System.IO.File. So I want to test that the function was called or not.
...
I have a method that performs an asynchronous service call. I call this class by passing in the callback.
public void GetRights(EventHandler<GetRightsCompletedEventArgs> callback)
{
ServiceClient client = new ServiceClient();
client.GetRightsCompleted += new EventHandler<GetRightsCompletedEventArgs>(callback);
client.GetRig...
I am writing unit tests against my ASP.NET MVC application, in particular I am testing an HtmlHelper extension method that I wrote. There is a line inside of the extension method:
var innerHtml = htmlHelper.ActionLink(text, action, controller, routeValues, null);
When I run this inside of my unit test, the href of the generated URL i...
I am creating a mock for my ITransformer interface.
public interface ITransformer
{
String Transform( String input );
}
I can create a mock that returns an given string based on a specific input:
var mock = new Mock<ITransformer>();
mock.Setup(s => s.Transform("foo")).Returns("bar");
What I would like to do is create a mock wit...
I have overridden the default NHibernate DefaultDeleteEventListener according to this source: http://nhforge.org/blogs/nhibernate/archive/2008/09/06/soft-deletes.aspx
so I have
protected override void DeleteEntity(
IEventSource session,
object entity,
EntityEntry entityEntry,
bool isCascadeDeleteEnable...
Some of my controller actions need to respond with different ViewResults depending whether or not they were called by an AJAX request. Currently, I'm using the IsAjaxRequest() method to check for this. When this method is called during a unit test, it throws an ArgumentNullException because the HTTP context is missing.
Is there a way t...
I don't have a lot of experience with the factory pattern and I've come across a scenario where I believe it is necessary but I'm not sure the I've implemented the pattern correctly and I'm concerned about the impact it's had on the readability of my unit tests.
I've created a code snippet that approximates (from memory) the essence of ...
Hi,
I'd like to use a mocking framework as well as an IOC framework with my latest project, based on subsonic 3 (ActiveRecord) and ASP.NET MVC.
I'd like to use Moq for mocking and Castle-Windsor for IOC.
Anyone got any advice or recommendations based on these choices? Any bumps in the road I should be aware of?
Chris
...
I'm starting to build up Unit Tests for a project we have. We've decided upon Moq to assist with the 'Mocking' of the repositorys as we dont want to run the tests against the live DB.
I'm obviously using Moq incorrectly, How would one write the GetMessage Test? The first 2 seem to work fine.
The return value of the GetMessage Test is ...
I'm trying to mock MouseButtonEventArgs.GetPosition() with Moq, but I keep receiving this error:
System.ArgumentException: Invalid setup on a non-overridable member:
m => m.GetPosition(It.IsAny<IInputElement>())
at Moq.Mock.ThrowIfCantOverride(Expression setup, MethodInfo methodInfo)
at Moq.Mock.<>c__DisplayClass12`2.<Setup>b__11()
...
Quite new to this mocking thing, I have a couple of questions.
Correct me if I'm wrong:
Mocking does not initialize the real method i.e. Mocking will not actually call the constructor of your class. Instead it does something like look at the signature of the class and create an object with that signature but with none of the methods fun...
In RhinoMocks or Moq, can properties on an object be set before the constructor is called?
I'm trying to test a method.
The class containing the method has some code in it's constructor which depends on some members being set, unfortunately there's no params in the constructor to set them so I must set them through a property. Is ther...
is moq 4.0 stable enough or should I just use 3.1?
...
when I use moq for my database factory, do I need to moq ALL the methods or just the ones that will be called?
or is that the beauty of using moq, where I just have to add moq methods for the ones I will use?
...
I have a ArticleController that displays a list of articles according to a category.
public ActionResult List(string categoryname)
{
MyStronglyTypedViewData vd = new MyStronglyTypedViewData();
DBFactory factory = new DBFactory();
categoryDao = factory.GetCategoryDao();
articleDao = factory.GetArticleDao();
...
I have a class that takes in a db factory.
public class ArticleManager
{
private IDAOFactor _factory;
public ArticleManager(IDAOFactory factory)
{
this._factory = factory;
}
}
Using moq, how do I create an instance of ArticleManager?
I tried:
var mockFactory = new Mock<IDAOFactory>();
ArticleMana...
My class has like 30-40 properties, and I really want to unit test.
But I have to create a moq instance (many of them, with different combinations etc).
Is there an easy way? This is real work!
My class can't be refactored, "trust me" (hehe, no really it can't, they are just properties of the object that are very tightly coupled).
...
Let's say I have this class:
public abstract class CustomerCollectionBase : Collection<Customer>{}
One of my classes under test accepts CustomerCollectionBase instance (it will be some subclass). In the method under test, this collection is enumerated via for loop and results are examined and processed.
like follows:
for(int i=0;i<_c...