开发者

NullReferenceException testing dataset in asp.net

I have written a 3 layer web site which has singleton repository for accessing database. My repositories use dataset for connecting to and query from database.

I want to test the site using Visual Studio 2010 Test project but when I create TableAdapter of dataset in repository I got following error in test application:

System.NullReferenceException: Object reference not set to an instance of an object.

the code work correctly when I use repository from the inside of site but in test application I got that error.

one of my repositories which I got that error is follow:

public sealed class VehicleRepository
{
    private readonly int gateCode;
    private readonly VehicleTableAdapter vehicleSet;
    private readonly VehicleTypeTableAdapter vehicleTypeSet;
    private static VehicleRepository instance;

    private VehicleRepository()
    {
        var configureTable = new ConfigurationTableAdapter();
--->>>      var configuration = configureTable.GetData().ToList();
            if (configuration.Count == 0)
                throw new UserInterfaceException("some message");
            if (configuration.Count != 1)
                throw new UserInterfaceException("some message");
            gateCode = configuration[0].GateCode;
            vehicleSet=n开发者_StackOverflow中文版ew VehicleTableAdapter();
            vehicleTypeSet=new VehicleTypeTableAdapter();
        }

    public static VehicleRepository GetInstance()
    {
        return instance ?? (instance = new VehicleRepository());
    }

    public Vehicle GetVehicleByPlaque(string plaque)
    {
        .....
    }

    private static Vehicle ConvertVehicleRowToVehicle(TransportCo.VehicleRow vehicleRow,TransportCo.VehicleTypeRow vehicleTypeRow)
    {
        ....
    }

    public void SaveOrUpdate(Vehicle vehicle)
    {
        ...
    }

    private static void UpdateVehicle(Vehicle vehicle)
    {
       ...
    }
}

I got the error in the --->>> line. does anyone know the problem?


I suspect that if you break that line into two steps and look at what GetData returns, you'll see it is null.


One Common catch ya with databases and test is that usually the connection string is stored in the configuration file of the web site. The web.config is usually not in play when executing the tests. That can easily lead to a scenario like the one you describe. So even though you know that it works in production it doesn't say anything about whether the connection is setup up correctly in test

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜