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

Mocking Test Data

3 Answers 159 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 22 Jun 2011, 09:34 PM
Ok, I'm not going to pretend I 100% still understand justmock, every sample always seems to be testing justmock if that makes sense :)

But we're running into an issue where our MSTests are failing due to profile bits changing.  So lets say my profile says I'm part of Program 1, and we write the test to check things against that using OpenAccess queries to the DB....but then someone changes me to Program 2, then the test starts to fail becasue the subsequent asserts were based on me being in Program 1.

So that being said...

Is it possible to have the mocking tool generate test objects\test data?

Let me jump ship for a second and throw another example

This is an Extension Method we have inside OpenAccess
public static MppProfile GetUser(this IQueryable<MppProfile> profiles, string userName) {
    return profiles.SingleOrDefault(x => x.AspnetUser.UserName.Equals(userName));
}

So if I have a test that says this
Assert.IsNotNull(_authDBContext.MppProfiles.GetUser("steve@medportal.ca"), "Failed on lowercase test  MppProfiles");

...how is that mockable?  When reading the examples it seems that I would mock the return from _authDBContext.MppProfiles.GetUser(), but if I did that isn't that just testing JustMock itself as what I really want to test is if profiles.SingleOrDefault isn't throwing an error?  Or am I looking at this the wrong way?

**Confused**

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 24 Jun 2011, 10:29 AM
Hi Steve,

Thanks again for sending the issue. On your first question:

Is it possible to have the mocking tool generate test objects\test data?

=> No, actually it is not possible to generate test objects using mocking tool. Mocking tool lets you create dummy stubs dynamically.

Secondly, you can mock extension methods just like any other methods. Here is an example from your snippet:

[TestMethod]
public void TestMethod1()
{
    var query = Mock.Create<IQueryable<MppProfile>>();
 
    const string targetUser = "steve";
    var expected = new MppProfile();
 
    Mock.Arrange(() => query.GetUser(targetUser)).Returns(expected);
 
    Assert.AreEqual(expected, query.GetUser(targetUser));
}


Here you also have to set  _authDBContext.MppProfiles with mocked instance in the following way.

Mock.Arrange(() => _authDBContext.MppProfiles).Returns(fakeQueryClass);

In addition, I have also attached the test project to let you have a look and hope this answers your question.


Kind regards,
Ricky
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 24 Jun 2011, 02:40 PM
Hey Ricky,
  Actually I'm more confused (sorry) :)

var query = Mock.Create<IQueryable<MppProfile>>();
Ok, so here, you're creating a fake version of MppProfile?

const string targetUser = "steve";
var expected = new MppProfile();
So then here, what's going on...setting the user to get to "steve" and creating the expected type to be MppProfile (the NOT fake\mocked version)?

Mock.Arrange(() => query.GetUser(targetUser)).Returns(expected);
So then here you say that when .GetUser is called, it'll always return a MppProfile object

Assert.AreEqual(expected, query.GetUser(targetUser));
...and then you assert that is what happens?

  I must have gotten something wrong while trying to break it down, please correct me :)  Because to me it again seems like a demo testing JustMock.  In this scenario I would want to test that GetUser actually calls the internal method to get a user and assert I have the correct person coming back from the DB.  But isnt this just faking GetUser?...like if I called query.GetUser("FAKEPERSON") it would still return the same fake object as query.GetUser(targetUser) even if I had no FAKEPERSON in the DB?
0
Ricky
Telerik team
answered on 30 Jun 2011, 05:20 PM
Hi Steve,
Thanks again for the reply. Moving on to your questions :
var query = Mock.Create<IQueryable<MppProfile>>();
Ok, so here, you're creating a fake version of MppProfile?

Here you are acutally creating  a mock of IQueryable<T> type which in your case  is IQueryable<MppProfile>.

Now since you have an extension method that takes IQueryable<MppProfile> as this argument , you can easily do the following:

Mock.Arrange(() => query.GetUser(targetUser)).Returns(expected);

This actually fakes the GetUser call that should return our target object no matter mocked/ non-mocked.


Finally during the assert
Assert.AreEqual(expected, query.GetUser(targetUser));

I am here checking if query.GetUser is returning the expected instance of MppProfile class.


In addition please try the latest internal build that we have released recently. If that does not solve your issue, it would be great if you send us a sample project that fails. This will help us investigate the problem further.

Kind Regards,
Ricky
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Ricky
Telerik team
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Share this question
or