Domanda

I am trying to test my code. There is a need to test the functionality of a module which works with NHibernate's Criteria. For example here is one of my unit tests.

[Fact]
public void test_name()
{
  var criteria = ... // creates mock criteria
  MyModule.DoWork(criteria); // adds restrictions to criteria

  Assert.True(/* check if criteria.Add() was called
  and what arguments were passed */);
}

If the MyModule.DoWork() adds a Restrictions.Eq(), there is no problem, because it returns a SimpleExpression and I can check the expression.PropertyName and expression.Value. But if instead it adds Restrictions.Not(Restrictions.Eq()) (which returns AbstractCriterion) there is no way to check the PropertyName and Value.

What do I do in such case? How to test against Restrictions.Not?

È stato utile?

Soluzione

You could for example test ToString() result of your AbstractCriterion ( which should be NotExpression with a nested private _criterion)

Anyway, I don't see it as a good idea. I may be off-topic here, but I would rather test the behavior of the result than its structure, or the result of ToString(). The main point being (IMHO) what the code does, more than its conformance to your knowledge of the target data-structure.

I guess it would be better to setup an in-memory database ( SqlLite comes to mind ) and test applying your restrictions on a test set. And it seems similar to (part of) the way NH is tested https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate.Test/Criteria/CriteriaQueryTest.cs

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top