This is a migrated thread and some comments may be shown as answers.

what is equivalent of moq.As in JustMock ?

6 Answers 93 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Md.Hasanuzzaman
Top achievements
Rank 1
Md.Hasanuzzaman asked on 10 Jan 2014, 12:34 PM
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.  
 

6 Answers, 1 is accepted

Sort by
0
Md.Hasanuzzaman
Top achievements
Rank 1
answered on 11 Jan 2014, 07:11 PM
Can you tell me how to write similar code  testing with async queries using JustMock
0
Kaloyan
Telerik team
answered on 14 Jan 2014, 10:17 AM
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.
0
Universitas
Top achievements
Rank 1
answered on 13 Jun 2014, 04:30 PM
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!
0
Kaloyan
Telerik team
answered on 18 Jun 2014, 01:41 PM
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.

 
0
Universitas
Top achievements
Rank 1
answered on 20 Jun 2014, 08:47 PM
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!
0
Kaloyan
Telerik team
answered on 25 Jun 2014, 01:42 PM
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.

 
Tags
General Discussions
Asked by
Md.Hasanuzzaman
Top achievements
Rank 1
Answers by
Md.Hasanuzzaman
Top achievements
Rank 1
Kaloyan
Telerik team
Universitas
Top achievements
Rank 1
Share this question
or