what is equivalent of moq.As in JustMock ?

7 posts, 0 answers
  1. Md.Hasanuzzaman
    Md.Hasanuzzaman avatar
    31 posts
    Member since:
    Apr 2011

    Posted 10 Jan 2014 Link to this post

    I am stuck to mock Entity Framework 6 asynchronous methods using JustMock. I searched in Google a lot but did not get any sufficient result. At last I got an example a testing with async queries but it is used Moq.

    I am try to convert this into JustMock but i can not find anything like .As<TInterface>() in JustMock. Please tell me what is equivalent of moq.As in JustMock ?

    I already spend more than 1 week for it. Please response as soon as possible.
    Thanks.  
     
  2. Md.Hasanuzzaman
    Md.Hasanuzzaman avatar
    31 posts
    Member since:
    Apr 2011

    Posted 11 Jan 2014 Link to this post

    Can you tell me how to write similar code  testing with async queries using JustMock
  3. DevCraft R3 2016 release webinar banner
  4. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 14 Jan 2014 Link to this post

    Hi Md.Hasanuzzaman,

    The "Moq" .As function is simply casting the mock object to an Interface. With JustMock you can directly cast the mock without the need of additional helper methods. In other words, you can do the following:
    Mock.Arrange(() => ((IDbAsyncEnumerable<Blog>)mockSet).GetAsyncEnumerator())
        .Returns(new TestDbAsyncEnumerator<Blog>(data.GetEnumerator()));

    You can check this article from our online documentation for more details.

    Further, I investigated the approach you have found, and managed to rewrite the test using JustMock. It looks like this:
    [TestMethod]
    public async Task GetAllBlogsAsync_orders_by_name()
    {
        var data = new List<Blog>
        {
            new Blog { Name = "BBB" },
            new Blog { Name = "ZZZ" },
            new Blog { Name = "AAA" },
        }.AsQueryable();
     
        var mockSet = Mock.Create<DbSet<Blog>>();
     
        Mock.Arrange(() => ((IDbAsyncEnumerable<Blog>)mockSet).GetAsyncEnumerator())
            .Returns(new TestDbAsyncEnumerator<Blog>(data.GetEnumerator()));
     
        Mock.Arrange(() => ((IQueryable<Blog>)mockSet).Provider).Returns(new TestDbAsyncQueryProvider<Blog>(data.Provider));
     
        Mock.Arrange(() => ((IQueryable<Blog>)mockSet).Expression).Returns(data.Expression);
        Mock.Arrange(() => ((IQueryable<Blog>)mockSet).ElementType).Returns(data.ElementType);
        Mock.Arrange(() => ((IQueryable<Blog>)mockSet).GetEnumerator()).Returns(data.GetEnumerator());
     
        var mockContext = Mock.Create<BloggingContext>();
        Mock.Arrange(() => mockContext.Blogs).Returns(mockSet);
     
        var service = new BlogService(mockContext);
        var blogs = await service.GetAllBlogsAsync();
     
        Assert.AreEqual(3, blogs.Count);
        Assert.AreEqual("AAA", blogs[0].Name);
        Assert.AreEqual("BBB", blogs[1].Name);
        Assert.AreEqual("ZZZ", blogs[2].Name);
    }

    I hope this helps. Also I want to thank you for posting this solution to our forums and grant you some Telerik points.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  5. Universitas
    Universitas avatar
    2 posts
    Member since:
    Mar 2014

    Posted 13 Jun 2014 Link to this post

    The solution you wrote seem to work only with the Profiler enabled.

    Is it possible to Mock the DbSet without the Profiler?

    Thank you and have a nice day!
  6. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 18 Jun 2014 Link to this post

    Hello Universitas,

    You are correct that the specific test runs successfully only with the JustMock profiler enabled. Nevertheless, I will check if there is a way to achieve similar testing functionality with the free version of JustMock. Unfortunately, for this I will require some additional time. I will get back to you immediately after I have results.

    I hope this is okay to you.

    Regards,
    Kaloyan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Universitas
    Universitas avatar
    2 posts
    Member since:
    Mar 2014

    Posted 20 Jun 2014 in reply to Kaloyan Link to this post

    Ok, thank you, we will wait for your answer.

    In fact, we use the paying version, so we have access to the profiler. However, our tests run much slower with the profiler enabled, so we are looking to turn it on only in case of absolute necessity, but we will need to mock Entity Framework DbSets all the time so that's why we want to do it without the profiler.

    Thank you again, and looking forward to your solution!
  8. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 25 Jun 2014 Link to this post

    Hello Universitas,

    I investigated the matter and it appears that the cast ((IDbAsyncEnumerable<Blog>)mockSet) fails when the profiler is not enabled. I have logged this in our bug tracking system and it should be fixed for future JustMock releases. We apologize for any inconveniences caused by this.

    Further, I suggest you to use the commercial JustMock until the fix is present in the profiler-free version.

    Thank you for the understanding.

    Regards,
    Kaloyan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
DevCraft R3 2016 release webinar banner