views:

95

answers:

3

I am using Linq to SQL in my project. I have a part of the code that calls

DataContext db = new DataContext()

This works as expected when running the website however when calling this from within my unit test I get an error object not set to an instance...

Do you know why this is?

I know I should Mock the data context for testing but there is only two tests that use this that I need completed for this stage of the project. Then I will go in and Mock.

I just don't see why it doesn't work.

Edit:

In my controller I have the line

                CandidateRegistrationViewModel viewModel = new CandidateRegistrationViewModel("PersonalDetails", candidate);

The Model has a member db:

 public class CandidateRegistrationViewModel
{
    private EmployDirectDataContext db = new EmployDirectDataContext();

This class then uses db to populate select boxes.

It all works when I run but in the unit test I get an error upon creating the datacontext.

[TestMethod]
    public void PersonalDetailsStepPostShouldRedisplayIfDOBSuppliedInWrongFormat()
    {
        // Arange
        var controller = CreateCandidateController("Dean");
        repository.Add(FakeCandidateData.CreateCandidate(controller.member.UserId()));

        FormCollection formCollection = FakeCandidateData.CreatePersonalDetailsStepFormCollection();
        formCollection["DOB"] = "24/2009/87"; //DOB in wrong format - should be dd/mm/yyyy

        controller.ValueProvider = formCollection.ToValueProvider();

        // Act
        ViewResult result = (ViewResult)controller.PersonalDetailsStep(formCollection);

        // Assert
        Assert.AreEqual("", result.ViewName); //ViewName is returned as empty if same as Action name
    }

Both of the projects have the same connection string in the app/web.config

<add name="EmployDirectDBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\EmployedDirectDB.MDF;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
A: 

Testing the Data Context falls more into the purview of an Integration Test. The mocks are more appropriate for your repository interface. Your repository will hold a valid reference to the DataContext object during your integration testing.

Robert Harvey
I have added more detail on my original post. I am not testing the datacontext but the test requires the datacontext runs just now until I ad mocking. But I want to add mocking after this milestone
dean nolan
Hm, well I don't see why it wouldn't work. It almost sounds like some kind of subtle problem with the testing engine. I'll give it some thought, but if it were me I would move this test into the next milestone where you will have the mocks.
Robert Harvey
A: 

Your unit test assembly probably does not have the right connectionstring compiled into the settings. That's why I always use:

var db = new MyDataContext(SomeConfigClassIMade.ConnString) {...}

so I can more tightly control how the connection string works,.

Dave Markle
I tried passing the connection string in here but still get the error
dean nolan
A: 

I an not sure why you would want to test the DataContext itself... (I may be wrong and I am sure someone will tell me if I am) but would you just test the DataAccess or Repository class that uses the DataContext...

Other than that it probably just doesn't have the correct connection string...

J.13.L
I have added code and so you can see I am not tewsting the data context. I have tried supplying the same connection string as my main project but still crashes
dean nolan