ʃʈɑɲ
ʃʈɑɲ

Reputation: 2694

Pass Expression<Func<Type, bool>> as TestCase

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)

Upvotes: 5

Views: 2101

Answers (2)

ʃʈɑɲ
ʃʈɑɲ

Reputation: 2694

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)

Upvotes: 3

Ivan G.
Ivan G.

Reputation: 5225

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

Upvotes: 0

Related Questions