4

Does anyone know how I can parameterize following:

[Test]
void SelectTest(Expression<Func<MyType, bool>> where)
{
    try
    {
        using (var db = new DataConnection("MyData"))
        {
            where = e => e.Status == Status.New;

            var data = db.GetTable<MyType>()
            .(where.Compile())
            .Select(e => e);

            Assert.IsNotEmpty(data);
        }
    }
    catch (Exception)
    {
        Assert.False(true);
    }
}

I tried adding a testcase like this:

[TestCase(e => e.Status == Status.New)]

But I'm getting the following error:

Expression cannot contain anonymous methods or lambda expressions.

What am I missing?

(I'm using linq2db and nunit)

grmbl
  • 2,514
  • 4
  • 29
  • 54

2 Answers2

3

Appearantly I can use NUnits TestCaseSource to pass Funcs.

See Pass lambda to parameterized NUnit test

My Solution:

public class SelectCollection
{
    public static IEnumerable<Expression<Func<Evaluation, bool>>> Evaluation
    {
        get
        {
            yield return (e) => e.Status == Status.New;
            yield return (e) => e.Id == 0;
        }
    }
}

Used as:

[Test]
[TestCaseSource(typeof(SelectCollection), "Evaluation")]
public void SelectTest(Expression<Func<Evaluation, bool>> where)
Community
  • 1
  • 1
grmbl
  • 2,514
  • 4
  • 29
  • 54
  • Yeah you can use test source, however can you see immediately how unreadable the test becomes? What's the chance it will be maintained by other devs? – Ivan G. Jan 26 '16 at 17:37
  • I agree but I'm the only one maintaining the tests.. In fact I'm the only one doing unit/integration tests. – grmbl Jan 27 '16 at 09:01
0

You cannot pass complex expressions as test arguments, only constant primitive types are supported.

Ivan G.
  • 5,027
  • 2
  • 37
  • 65