vendredi 29 avril 2016

Testing properties with private setters

Currently in a part of my project a domain object like below exists:

public class Address
{
    public virtual string HouseName { get; set; }

    public virtual string HouseNumber { get; set; }

    public virtual string RoadName { get; set; }

    public virtual string Postcode { get; set; }

    public virtual string District { get; private set; }
}

The District property is a calculated column in the database. Due to the requirements of the data access provider (Entity Framework) a private set is required. This is fine in normal program flow as the District never needs to be set, due to it being updated when an Address record is saved/updated in the database

A method that I want to test looks like this:

public IEnumerable<House> GetHousesWithinSameDistrict (int addressId)
{
    var addressToMatch = _addressRepository
      .FirstOrDefault(address => address.Id == addressId)

    return _houseRepository
      .Where(house => house.Address.District == addressToMatch.District)
}

This is causing me problems when I try to set up the addressToMatch variable, as I am unable to set the District property and as such I cannot write a test to check correct matching addresses are returned.

How should I go about setting up an Address object for this test?

Aucun commentaire:

Enregistrer un commentaire