chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

I'm trying out some of the new LINQ code and trying to use TypeMock with it. I have 4.1 install. But I keep getting an error.

Error Message
Test method Controller2008.Test.ProductControllerTest.GetCategoriesTest threw exception: TypeMock.TypeMockException:
*** Cannot return a value for ProductsDataContext.get_Categories() because no value was set. use recorder.Return().
*** Note: Cannot mock types from mscorlib assembly..


Unit Test Method
        [TestMethod()]
        [ClearMocks]
        [VerifyMocks]
        public void GetCategoriesTest()
        {
            ProductController target = new ProductController();
            List<Category> expected = new List<Category>(){
                new Category { CategoryID = 1, CategoryName = "Test1", Description = "Testing1"},
                new Category { CategoryID = 2, CategoryName = "Test2", Description = "Testing2"},
                new Category { CategoryID = 3, CategoryName = "Test3", Description = "Testing3"}
            };
            List<Category> actual;

            var dummyData = new[] {new { CategoryID = 1, CategoryName = "Test1", Description = "Testing1"},
                                   new { CategoryID = 2, CategoryName = "Test2", Description = "Testing2"},
                                   new { CategoryID = 3, CategoryName = "Test3", Description = "Testing3"}};

            ProductsDataContext db = new ProductsDataContext();

            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                var query = from cat in db.Categories  //**Breaks on this line.
                            select cat;

                recorder.Return(dummyData);
            }

            actual = target.GetCategories();
            
            Assert.AreEqual(expected, actual);
        }


Do I need to have the enterprise version to run this code? I'm using the Professional version.

Thanks,
Eric
asked by eschatzy (3.8k points)

2 Answers

0 votes
Hi,
Thanks for reporting this, it is a bug :twisted:
We will fix it and send you the fix.

:arrow: Workaround: use db.Categories outside the linq query

Tip :idea:, use either VerifyMocks or ClearMocks not both (if clear is run before verify - it will always pass)

[TestMethod()]
[VerifyMocks]
public void GetCategoriesTest()
{
  ProductController target = new ProductController();
  List<Category> expected = new List<Category>(){
     new Category { CategoryID = 1, CategoryName = "Test1", Description = "Testing1"},
     new Category { CategoryID = 2, CategoryName = "Test2", Description = "Testing2"},
     new Category { CategoryID = 3, CategoryName = "Test3", Description = "Testing3"}
  };

  ProductsDataContext db = new ProductsDataContext();

  using (RecordExpectations recorder = RecorderManager.StartRecording())
  {
     var categories = db.Categories;
      recorder.ReturnDefaultImplemetation();
     var query = from cat in categories select cat;
      recorder.Return(expected);
  }

  List<Category> actual = target.GetCategories();
           
  Assert.AreEqual(expected, actual);
}
answered by scott (32k points)
0 votes
Hi everyone,

We've got some fixes for mocking LINQ in version 4.3. You can download it here: https://www.typemock.com/Downloads.php
answered by gilz (14.5k points)
...