These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Unit testing ICriteria calls
PostPosted: Thu Aug 17, 2006 11:47 pm 
Newbie

Joined: Sat Oct 15, 2005 3:30 am
Posts: 17
Hi,

I was wondering how people are Unit testing their ICriteria calls.

In my tests I am using a Mock Object library (NMock2 in this case but it shouldn't matter which), and setting up my test so that I am having a mock ICriteria returning from an ISession.GetCriteria call.

Now I want to set expectations on ICriteria, to check that the expected Expressions are being added.

eg.

// check that an Equals Expression is added to ICriteria
Expect.Once.On(mockCriteria).method("Add").With(Expression.Eq("SomeProp", "SomeValue"));

- this doesn't work, I always get an expectation exception from NMock.

I think the prob is with Expression.Eq being static.

Anyway, I was wondering if/how others are unit testing ICriteria calls.

Thanks,

Adam.


Top
 Profile  
 
 Post subject: Solution to Unit Testing ICriteria
PostPosted: Sun Aug 20, 2006 11:52 pm 
Newbie

Joined: Sat Oct 15, 2005 3:30 am
Posts: 17
OK, I have a solution, it involves doing matching on property values that are inside the criteria. The method CreateCriteriaMatcher does that work...

Here is a full example:
Code:
using System;
using NUnit.Framework;
using NHibernate;
using NHibernate.Expression;
using NMock2;
using NMock2.Matchers;
using System.Collections;

namespace Foo
{
   [TestFixture]
   public class SearcherTest
   {
      [Test]
      public void SearcherAppliesCorrectCriteria()
      {
         IList irrelevantList = null;

         Mockery mocks = new Mockery();
         ISession mockSession = (ISession)mocks.NewMock(typeof(ISession));
         ICriteria mockCrit = (ICriteria)mocks.NewMock(typeof(ICriteria));

         NMock2.Expect.Once.On(mockSession).Method("CreateCriteria").With(typeof(Bar)).Will(Return.Value(mockCrit));
         NMock2.Expect.Once.On(mockCrit).Method("Add").With(CreateCriteriaMatcher(Expression.Eq("Prop1", "adam")));
         NMock2.Expect.Once.On(mockCrit).Method("List").WithNoArguments().Will(Return.Value(irrelevantList));

         Searcher searcher = new Searcher(mockSession);
         IList result = searcher.DoStuff("adam");

         Assert.AreEqual(irrelevantList, result);
      }

      private Matcher CreateCriteriaMatcher(SimpleExpression expression)
      {
         Matcher valMatch = new PropertyMatcher("Value", new EqualMatcher(expression.Value));
         Matcher nameMatch = new PropertyMatcher("PropertyName", new EqualMatcher(expression.PropertyName));
         Matcher andMatcher = new AndMatcher(valMatch, nameMatch);
         return andMatcher;
      }
   }

   public class Searcher
   {
      ISession session;

      public Searcher(ISession session)
      {
         this.session = session;
      }

      public IList DoStuff(string name)
      {
         ICriteria crit = session.CreateCriteria(typeof(Bar));

         crit.Add(Expression.Eq("Prop1", name));
         return crit.List();
      }
   }

   public class Bar
   {
   }
}
[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.