xxviktor
xxviktor

Reputation: 3

Unit test NullReferenceException with string property problem

I'm writing unit test for ASP.MVC 3 app with EF 4.0 and I have problem with System.NullReferenceException during the testing. I'm testing this method in service layer:

public IQueryable<Pricing> GetPricing(int categoryID)
    {
        var query = from t in _repository.GetAllPricing()
                    where t.FK_Category == categoryID
                    where t.Status.Equals("1")
                    select t;
        return query;
    }

It's working fine. But when Status equals to null and I call

svc.GetPricing(1).Count();

in the test method, then it throws exception. I'm using fake repository and other (empty) string works well.

I've tried to use pricing.Status = Convert.ToString(null); instead of pricing.Status = null; but this doesn work either.

Upvotes: 0

Views: 458

Answers (1)

Jackson Pope
Jackson Pope

Reputation: 14640

The problem is that you can't call .Equals on a null reference - it will as you've experienced throw a NullReferenceException.

Instead you can call the equality operator:

public IQueryable<Pricing> GetPricing(int categoryID)
{
    var query = from t in _repository.GetAllPricing()
                where t.FK_Category == categoryID
                where t.Status == "1"
                select t;
    return query;
}

Upvotes: 1

Related Questions