Telerik Forums
JustMock Forum
5 answers
238 views
Hi!

Suppose I want to mock a method with the following signature:

public

bool MyHelperMethod(int arg1, out DataSet ds1, out DataSet ds2, out DataSet ds3, out DataSet ds4)

Is there a way to use the "Returns" method to return a value that depends on the argument values passed in, rather than returning a hardcoded value?

I am thinking of something along the lines of:

Mock.Arrange(() => helper.MyHelperMethod(Arg.AnyInt, out set1, out set2, out set3, out set4))

.DoInstead(() =>

{
// mock implementation omitted 

})

.Returns(
(int arg1, out DataSet ds1, out DataSet ds2, out DataSet ds3, out DataSet ds4)
=> /* some return value computed from the arguments */
);

If this is not possible (due to the "out" parameters in the method signature), can I do what I want to do by writing multiple Arrange statements, each matching on different parameter values, and then return a different value in each of these Arrange statements?

Thanks,
Asim

Ricky
Telerik team
 answered on 28 Jul 2011
1 answer
108 views

If you are unit testing a method that gets an Arranged mocked property and, then, sets that property, how do you verify the results at then end?

For example, let's say we want to unit test a method that looks like:

public void MyMethod()
{
    if (_view.SelectedObject != null)
        _view.SelectedObject = null;
}

I would expect to be able to write a test similar to:

[TestMethod]
public void MyMethodTest()
{
    var view = Mock.Create<iView>();
    var testObject = new Thingy();
  
    Mock.Arrange(() => view.SelectedObject).Returns(testObject);
    Mock.ArrangeSet(() => view.SelectedObject = null);
      
    objectUnderTest.MyMethod();
  
    Assert.IsNull(view.SelectedObject); 
}

The assert fails because the mocked property return is still in effect and you get testObject.  What's the proper way to write this test?  Thanks!

Ricky
Telerik team
 answered on 27 Jul 2011
5 answers
188 views
Ok, so we have an asp.net server control

The server control internally calls a method to Load the Authenticated User.  It HAD System.Web.HttpContext.Current hardcoded in this method, but I extracted that out to a property to allow me to pass in my own mocked version (I assume thats what JustMock does)?

protected override MppProfile GetAuthenticatedProfile()
{
    if (this.LoadAuthenticated && _authenticatedProfile == null) {
        try
        {
            if (this.Context.User != null)
            {
                try {
                    foreach (MembershipProvider mp in Membership.Providers) {
                        MembershipUser user = mp.GetUser(Context.User.Identity.Name, false);
                        if (user != null) {
                            _authenticatedProfile = AuthDBContext.MppProfiles.SingleOrDefault(x => x.AspnetUser.UserName == user.UserName);
                            break;
                        }
                    }
                } catch (ConfigurationErrorsException ex) {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    throw new Exception(ex.BareMessage);
                }
            }
        }
        catch (ArgumentException ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
            // No authenticated user
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
            throw new InvalidOperationException("Membership must be configured to load the authenticated user. Either configure a membership provider or set LoadAuthenticated=false.");
        }
    }
    return _authenticatedProfile;
}

So the question is...how do I go about Mocking this?  Because I need this Unit Test to pass :)  I want GetAuthenticatedProfile to return me an MppProfile object of my choosing as this test doesnt need to go through all of the MembershipUser bits.

Am I even doing this right?  Should it be simpler in that I pass in an isTest bool and just ignore this part of the method if isTest is true?
Ricky
Telerik team
 answered on 21 Jul 2011
1 answer
116 views
Hi There,
     Currently I've facing parameters limitation in JustMock,
I've several functions which have more than 9 parameter, (eg: 10 parameters)

When I'm mock my function, up to 9 parameter it is ok to compile, when my functions have 10 or more parameter JustMock give me error. as following
Error    17    Delegate 'Telerik.JustMock.Action2' does not take 10 arguments   



sample function
//Try to Produce Error of more than 9 Parameters for JustMock
void GetTestFunc(int param1, int param2, int param3, int param4, int param5,
 
string sParam6, string sParam7, string sParam8, string sParam9, string sParam10)
{
string _nothing= sParam10;
}
 
//Inside Test Project 
        Mock.Arrange(()=> GetTestFunc(Arg.AnyInt, Arg.AnyInt,Arg.AnyInt, Arg.AnyInt,Arg.AnyInt, Arg.AnyString,Arg.AnyString,Arg.AnyString,Arg.AnyString,Arg.AnyString))
                .DoInstead((int one, int two, int three, int four, int five, string a, string b, string c, string d, string e) =>
                    {
                        string _a = e;
                    });
Ricky
Telerik team
 answered on 20 Jul 2011
1 answer
102 views
Good day,

I'm wondering if there anyway of enabling JustMock so that it doesn't disable on it own?  I've enable Justmockfor for one test and on the next test run, JustMock has been disabled.  I've try the suggestion in http://www.telerik.com/community/forums/justmock/general-discussions/why-is-the-enable-disable-the-profiler-an-ide-setting.aspx but it doesn't seem to always work.

Thanks for any suggestion that you might have,

James
Mihail
Telerik team
 answered on 18 Jul 2011
1 answer
101 views
Hi

We are experiencing some problem with some tests where mocking in one test seemed to interfere with another. I.e there is a test class that has 32 tests. When all tests are run one of them fails. When this one test is run on its own it passes. It seems that one of arrangements is not working. It's very difficult to reproduce that in controlled environment and I'm not allowed to send all sources.

What could cause that and what should I look at. I'm asking for some guidelines that could help me to nail the problem. Alternatively I would appreciate if someone could help me directly with the problem. I'm happy to organise a Live Meeting/Webex/Whatever session to show the problem.

Thanks in advance.
Ricky
Telerik team
 answered on 15 Jul 2011
2 answers
317 views
I have a problem with this code:

public interface IProcessDataPersister
{
List<TaskWarning> GetTaskWarnings(Guid taskId);
}
 
//in a method
var localPersister = Mock.Create<IProcessDataPersister>();
Mock.Arrange(() => localPersister.GetTaskWarnings(new Guid("{00000000-0000-0000-0001-000000000003}")))
.Returns(new List<TaskWarning>() { new TaskWarning(new Guid("{00000000-0000-0000-0001-000000000003}")) { EscalationLevel = 0 } })
.MustBeCalled();

Mock.Arrange(...) throws the following exception:
System.NullReferenceException occurred
  Message=Object reference not set to an instance of an object.
  Source=Telerik.JustMock
  StackTrace:
       at .™.(Type valueType, Object value) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Utility.cs:line 334
  InnerException: 


If I change the Arrange method to use Arg.IsAny<Guid>() instead of the explicit value there is no exception and everything works fine.
I am using the latest internal JustMock Free Edition build 2011.1.620.0.
Also test it on the official build and the results were the same.
Hope that will help you solve the problem.
Viktor zhivkov
Top achievements
Rank 1
 answered on 01 Jul 2011
3 answers
258 views
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**
Ricky
Telerik team
 answered on 30 Jun 2011
6 answers
130 views
After installing JustMock on my work machine I'm getting the following error when in debug

 

A first chance exception of type 'System.UnauthorizedAccessException' occurred in Telerik.CodeWeaver.Hook.dll

I've uninstalled 2 times. This works great for my home pc. Is there any permission that I need to insure on this dll. In my project I only have a reference to Telerik.JustMock. Thanks in advance!

brian26
Top achievements
Rank 1
 answered on 28 Jun 2011
2 answers
125 views
Hi everyone.

I'm having a weird problem with the the JustMock.Assert() method and "Running All Tests" in the solution.

I wrote a solution having < 50 unit tests. In these tests, I'm using the Mock.Assert(), and the .OccursOnce() quite often. Now if I run all these tests at once using the "Run All Tests in Solution" Button some of these fail. But when I run the failing tests once again, they pass.
This means, that I always have to run the unit test twice to get my solution green.

It seems, that this problem appears, when Mocking the same objects in different test cases.

One sample exception of such test would be: 
Test method [methodname] threw exception: 
Telerik.JustMock.MockAssertionException: Expected SAPtoADZipper.Zip(List`1) call on the mock should be once, but it was called 0 time(s).

I'm using Just Mock version 2011.1.315.0, and Microsoft.VisualStudio.TestTools.UnitTesting in Visual Studio 2010 with C# in the 3.5 .net Framework.

Does anyone have a hint for me, how to resolve this?
Maybe there's a way to mark the tests to run in an isolated Thread or something.

Thanks!


Daniel Eggers PO 4900000041
Top achievements
Rank 1
 answered on 27 Jun 2011
Narrow your results
Selected tags
Tags
+? more
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?