-3

I am using LinqToSQL in my project.As pert of my unit testing, I am trying to mock datacontext which is part of another assembly which is referenced in the ASP.NET WEB API project.

Based on the following URL: http://weblogs.asp.net/rashid/100-unit-testable-linq-to-sql-repository

I tried to create a partial class for the datacontext but somehow it is not working.

Can anyone help me to know how to mock the datacontext class which is part of another referenced assembly.

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • *somehow* isn't very descriptive. What exactly isn't working, what mocking framework are you using, what are you trying to test... What does your test look like.. What does your code look like. What language are you using.. At the moment your question is little more than I have tried to implement something from the internet, why doesn't it work? – forsvarir Feb 04 '16 at 16:07
  • sorry for missing the details. I am using mstest and VS2013 and C# to write the unit test. I am finding hard to mock the datacontext that is part of another assembly. Can you please help me to provide some code sample. – santosh kumar patro Feb 04 '16 at 16:11
  • So.. `var database = new Mock("A dummy connection string")` doesn't compile? – forsvarir Feb 04 '16 at 16:24
  • we need your code, in order to help you out! What have you tried? – Jocke Feb 04 '16 at 20:33

1 Answers1

0

Right from what I can gather from skim reading that article is that it's mostly call assertion and there are no tests around queryable results. IMO these tests are pretty meaningless and more over when you start putting in meaningful tests it wont work because Linq-to-sql is not mockable, DbContext is not mockable, can't use pocos, etc. This article was written in 2009 and Linq-to-sql is not a current product.

How big is the project? If at all possible move to EF6 and utilise either IDbSet to create a FakeDbSet or Mock the DbSets themselves and alot of the methods are virtual now. Have a look at this Q&A:

How are people unit testing with Entity Framework 6, should you bother?

If you search for Unit testing EF6 you find what your're trying to achieve much easier.

If you really can't change the Ling2Sql then use a façade pattern and compose you DbContext inside this. The façade would expose the DbSet are IQueryable and wrap up any method calls. This means though that you would have to create methods and properties that are virtual and then you can mock the façade. Alternatively you could also create an interface for the facade which you can also use for mocking. This would make any complex queries you write completely testable but you are creating code that calls other code. You would also have to watch the lifestyle of the façade as this is newing up the DbContext. This pattern is used to get round testing where AppSettings are involved, it's the same thing really. You just creating something that is mockable as a wrapper for something that isn't.

Community
  • 1
  • 1
jolySoft
  • 2,948
  • 2
  • 30
  • 34
  • sorry for the brief details in my question above. In my case I have a component which uses linqtosql and developed by another team. I am referencing the component as dll in my application. Here in this case I am not able to find the best possible way to mock the datacontext which is part of the component. – santosh kumar patro Feb 10 '16 at 13:13
  • @santoshkumarpatro Good point! I've updated the answer to reflect you situation but it's not quick. – jolySoft Feb 10 '16 at 13:25
  • Thanks a lot for the help. – santosh kumar patro Feb 10 '16 at 14:04