Mocking Linq – Preview
We are working on our .NET 3.5 Support. There are many new feature that have been added, we will take a look at how to mock a linq statement
For the following code in our application
Northwind db = new Northwind(connStr) ; var query = from c in db.Customers where c.City == "Sarasota" select new { c.Name, c.City };
Here is how to mock this statement and return fake customers
[TestMethod] [VerifyMocks] public void IsolateLinq() { // create fake return values var fake = new[] { new {Name="Fake 1",City="NY"}, new {Name="Fake 2",City="London"}}; using (RecordExpectations r = new RecordExpectations()) { // don't hit the database, instead return test customers Northwind db = new Northwind(connStr) ; var answer = from c in db.Customers where c.City == "not-used" select new { c.Name, c.City }; r.Return(fake); } CallClassUnderTest(); // Continue with test }
Comments about this syntax are welcome
4 Comments to “Mocking Linq – Preview”
Recent Posts
- Unacceptable: Unit testing will take 20 years to catch on
- The 4 reasons why we DIDN’T choose Oslo
- Typemock Academy Launch
- The First Rule to Software Craftsmanship
- Goal-driven Development
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- May 2010
- April 2010
- March 2010
- February 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- May 2008
- April 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006

Can this Mocking Linq be done with the community version, or is a higher level required?
Larry,
The Enterprise Edition is required for Linq.
This is not true!
Linq can be mocked with the community version.
All you need to do is mock [Your]DataContext, and pass an IQueryable instead of your Table property (Use ExpectGetAlways(propertyName, mockList.AsQueryable()).
HTH,
Assaf.
interesting post thx