This is a migrated thread and some comments may be shown as answers.

Mocking LINQ Repository Insert operation

3 Answers 135 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 20 May 2012, 11:41 PM
I started using JustMock to mock my Linq To SQL (L2S) based repository. Everything works well for my Get operations; however, I am getting a consistent error when I attempt to mock for Insert operations.  The following is my code: 
var context = Mock.Create<MyDataContext>();
          var repository = new CustomerRepository(context);
          var customer = new Customer
              {
                  CustomerId = 0,
                  CompanyId = 1,
                  CustomerNo = "FAKE4",
                  FullName = "Isaac Fake4",
                  LastName = "Fake",
                  FirstName = "Isaac",
                  Sex = "M",
                  Birthdate = new DateTime(2000, 12, 1),
                  IsRecordActive = true,
                  IsRecordDeleted = false,
                  ModifiedOn = DateTime.Now,
                  ModifiedBy = "System.User"
              };
          
          Mock.Arrange(context.SubmitChanges).DoInstead(() =>
          { customer.CustomerId = 5; });
          
          //Act
           
          var actual = repository.Add(customer);
   
          //Assert
          Assert.AreEqual(5, actual.CustomerId);

The call to the Add method in the Customer repository is as follows:
public Customer Add(Customer customer)
       {
            
           this.context.Customer.InsertOnSubmit(customer);
           this.context.SubmitChanges();
           return customer;
       }

The system generates a NullReferenceException error on the InsertOnSubmit call and never gets to the SubmitChanges line.

I moved the same code to a console app for testing and it executed correctly (minus the JustMock embellishments of course).  What is the correct way to utilize JustMock to get the above code to test properly.

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 23 May 2012, 08:45 PM
Hi Dave,
Thanks again for contacting us. So far from your test it looks to me that you have a predefined Customer class but you haven’t assigned it to the target context.

You need to do the following before you can call InsertOnSubmit from this.context.Customer. Since MyDataContext is mocked and by default it should return null for get_Customer call.
Mock.Arrange(() => context.Customer).Returns(customer);

Hope this answers your question.

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Dave
Top achievements
Rank 1
answered on 23 May 2012, 10:09 PM
Ricky,

Thank you for your response.  Unfortunately, I tried that already.  It gives me an InvalidCastException.  The context.Customer object is of System.Data.Linq.Table<> and the customer object is a POCO entity representation.   I can; however, do the following:
Mock.Arrange(() => context.Customer).ReturnsCollection(GetFakeCustomers());
where GetFakeCustomers() is a collection of customer objects that returns IEnumerable.   However, that still does not work and returns the same error of my original post.   

Can you offer any further insight into this issue?

Thanks.
0
Ricky
Telerik team
answered on 25 May 2012, 03:58 PM
Hi Dave,

Thanks again for contacting us.  

Considering the fact that you are still having the issue, I would request you to send me the context class and necessary files to run the test that is failing. 

I will then debug through the issue and send you a possible solution.
 

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
General Discussions
Asked by
Dave
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Dave
Top achievements
Rank 1
Share this question
or