Mocking LINQ Repository Insert operation

4 posts, 0 answers
  1. Dave
    Dave avatar
    2 posts
    Member since:
    May 2012

    Posted 20 May 2012 Link to this post

    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.

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 23 May 2012 Link to this post

    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 >>

  3. DevCraft R3 2016 release webinar banner
  4. Dave
    Dave avatar
    2 posts
    Member since:
    May 2012

    Posted 23 May 2012 Link to this post

    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.
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 25 May 2012 Link to this post

    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 >>

Back to Top