Telerik Forums
JustMock Forum
3 answers
236 views
Hi I have this situation.

Public Class PrivateMethodDoInstead
    Private Function GetSomeQuantity(ByRef quantity As Long) As Boolean
        '....get quantity from the database code would be here
        Return quantity > 0
    End Function
 
    Public Function HandleRecords() As Boolean
        Dim quantity As Long
        Dim returnValue As Boolean = GetSomeQuantity(quantity)
 
        If returnValue Then
            StoreTotals(quantity)
        End If
 
        Return returnValue
    End Function
 
    Private Sub StoreTotals(quantity As Long)
        ' do something
    End Sub
End Class

I would like to Mock my private function GetSomeQuantity.
It is private. When it is public it would work by doing this:

Mock.Arrange(Function() testInstance.GetSomeQuantity(
     Arg.IsAny(Of Long))
    ).DoInstead(Function(ByRef quantity As Long) As Boolean
                    quantity = 5
                    Return True
                End Function).Returns(True)

But how can I do this for a private function?
I did try it using the nonpublic, but it keeps giving me an error that it cannot resolve the method and that I should check my arguments.

Any help would be appreciated.

Thanks,

Wietze
Garo
Telerik team
 answered on 26 Dec 2016
1 answer
144 views

I had a first try with JustMock on a Linux build machine (Jenkins) and xunit (Mono 4.6 is installed there). Unfortuntely, I get the following exception (one of them):

01.Tnsa.Foundation.Test.Composition.Internal.CompositionContextServiceProviderAdapterTest.GetService_ExportFactories_enumerable_generic_2_success [FAIL]
02.  System.Runtime.Remoting.RemotingException : Cannot get the real proxy from an object that is not a transparent proxy.
03.  Stack Trace:
04.      at System.Runtime.Remoting.RemotingServices.GetRealProxy (System.Object proxy) [0x0000b] in <94fd79a3b7144c54b4cb162b50fc7761>:0
05.      at Telerik.JustMock.Core.TransparentProxy.MockingProxy.GetRealProxy (System.Object instance) [0x00005] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
06.      at Telerik.JustMock.Core.TransparentProxy.MockingProxy.GetMockMixin (System.Object instance) [0x00000] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
07.      at Telerik.JustMock.Core.MocksRepository.GetMockMixinFromAnyMock (System.Object mock) [0x00000] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
08.      at Telerik.JustMock.Core.MocksRepository.GetMockMixin (System.Object obj, System.Type objType) [0x00000] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
09.      at Telerik.JustMock.Core.MocksRepository.ConvertExpressionToCallPattern (System.Linq.Expressions.Expression expr, Telerik.JustMock.Core.CallPattern callPattern) [0x00564] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
10.      at Telerik.JustMock.Core.MocksRepository.Arrange[TMethodMock] (System.Linq.Expressions.Expression expr, System.Func`1[TResult] methodMockFactory) [0x00049] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
11.      at Telerik.JustMock.Helpers.FluentHelper.DoArrange[TContainer] (System.Object obj, System.Type objType, System.Linq.Expressions.LambdaExpression expression, System.Func`1[TResult] containerFactory) [0x00036] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
12.      at Telerik.JustMock.Helpers.FluentHelper+<>c__DisplayClass2`2[T,TResult].<Arrange>b__0 () [0x0002e] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
13.      at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T] (System.Func`1[TResult] guardedAction) [0x0000c] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
14.      at Telerik.JustMock.Helpers.FluentHelper.Arrange[T,TResult] (T obj, System.Linq.Expressions.Expression`1[TDelegate] expression) [0x00020] in <45764d21e4e34d73b89fbb0a6b2e6054>:0
15.      at Tnsa.Foundation.Test.Composition.Internal.CompositionContextServiceProviderAdapterTest.GetService_ExportFactories_enumerable_generic_2_success () [0x0006f] in <e1aa8dcdd51045fda2db8ea7350c0c44>:0
16.      at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
17.      at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in <94fd79a3b7144c54b4cb162b50fc7761>:0

 

Is this scenario supported on Linux/Mono at all? Can I do something to make this work?

Thanks, Ioan

Svetlozar
Telerik team
 answered on 12 Dec 2016
4 answers
180 views

For some reason, the ReturnsMany method does not work for me. I have the follwing code:

List<ORGANIZATION> existingOrganizations = new List<ORGANIZATION>()
            {
                new ORGANIZATION() {ID = 1, NAME = "Organization1", ISINCLUDEDINSAMPLE = false},
                new ORGANIZATION() {ID = 2, NAME = "Organization2", ISINCLUDEDINSAMPLE = false},
                new ORGANIZATION() {ID = 3, NAME = "Organization3", ISINCLUDEDINSAMPLE = false},
            };

 

container.Arrange<IOrganizationRepository>(or => or.GetById(Arg.AnyLong)).ReturnsMany(existingOrganizations);

 

And the exception I'm getting:

System.InvalidCastException : Unable to cast object of type 'System.Collections.Generic.List`1[Cpims.WFM.Domain.Organizations.ORGANIZATION]' to type 'Cpims.WFM.Domain.Organizations.ORGANIZATION'.
   at Castle.Proxies.IOrganizationRepositoryProxy.GetById(Int64 primaryKey, Expression`1[] includeNavigationProperties)

 

Svetlozar
Telerik team
 answered on 08 Dec 2016
1 answer
552 views
Trying to arrange Encoding.UTF8.GetBytes to return some bytes is throwing an NotImplementedException with the message "You can't call the original implementation of a method that does not have one (abstract or interface method). I'm able to successfully mock out Encoding.UTF8.GetString in the exact same way, so this confuses me. Is it a bug?
Svetlozar
Telerik team
 answered on 16 Nov 2016
11 answers
1.0K+ views
Hi.
I am slightly confused by the JustMock integration with visual studio.

Will tests start failing if I disable the "Enable Profiler" setting?
How do I control this setting on the CI build server?

It seems to me that wether or not the JustMock library shall use the .NET profiler API should be an attribute or passed parameter in the individual test and not a global IDE setting. Then I should organize my tests so that I don't run long-running ones on the quick cycles.
The profiler should be dynamically loaded if the unit tests demanded the profiler. AFAICT, this information should be contained in the unit tests.

Why was the approach chosen to have this as a manual setting in the IDE? I want my testcode to be self specified. I don't want to explain to a colleague that the reason why my unit tests are failing on his computer is because of a setting. That's a definite no-no in TDD.

Why was this approach chosen?
Svetlozar
Telerik team
 answered on 24 Oct 2016
1 answer
113 views

In a method like

void ReturnIfObjectIsNull()
        {
            var obj = new SomeObject();
 
            if (obj == null)
                return;
 
            // Do other stuff..
        }

 

How do I assert that the early return fired (without asserting that the stuff that would have happened afterword never occurred).
Asserting the object is null is not enough. I would need to assert that he function returned early when the object was null.

Thanks.

Svetlozar
Telerik team
 answered on 19 Oct 2016
4 answers
2.0K+ views
Hi,

I am new to justmock and need help in mocking the below code.I have a WCF data service method which process the service URI and gives out a XML result. I am not sure on how to mock the HTTP  request and response, especially assigning content type etc.Can anybody help out with below.Thanks in advance.

// Obtain an HTTP web Request object for the desired URI
        var Request = WebRequest.Create(strUri) as HttpWebRequest;
        if (Request != null)
        {
            Request.KeepAlive = false;
            Request.Method = "GET";
            Request.ContentType = "application/ems-v3+xml";
 
            // Obtain the HTTP web response
            using (var Response = Request.GetResponse() as HttpWebResponse)
            {
                // Read the response text stream (the XML) into a local string
                if (Response != null)
                    using (var ResponseStreamReader =
                        new StreamReader(Response.GetResponseStream()))
                    {
                        //get XML Result
                        result = ResponseStreamReader.ReadToEnd();
                    }
 
            }
 
        }

Thanks,
Divya
netcana
Top achievements
Rank 1
 answered on 12 Oct 2016
1 answer
98 views

Hi All,

 

I have started using JustMock Few days back in my solution. and Now I am trying to Integrate this into my Build Definition which is in VSO.

 

Any Idea how we can Integrate JustMock with my Solution.

 

Note: I cannot Install this JustMock on my Build Server.

Svetlozar
Telerik team
 answered on 07 Oct 2016
1 answer
76 views

JustMock R3 2016 (2016.3.914.2)

The below test will pass when the second ArrangeSet should fail instead. I tried reporting the issue through VS2013 but I got a CommunicationException error: The server did not provide a meaningful reply.

[TestMethod]
public void Test()
{

    var pageMock = Mock.Create<IPage>()
    Mock.ArrangeSet(() => pageMock.WebControl1.Enabled = false).OccursNever();
    Mock.ArrangeSet(() => pageMock.WebControl2.Enabled = false).MustBeCalled();

    Mock.Assert(pageMock);
}

Svetlozar
Telerik team
 answered on 20 Sep 2016
2 answers
100 views

JustMock Q2 2016 SP1 (2016.2.713.2)

I have the JustMock license. I installed JustMock (the JustMock_2016_2_713_2_Dev.msi file). Also I downloaded and extracted the `JustMock_2016_2_713_2_Help3.zip` file. Now I launch the `Install_TelerikJustMock.bat` file but I get the error message:

 

> Error: It isn't possible to find the specified section or parameter in the register.
> Ensuring same version not registered...
> ""\HelpLibManager.exe"" isn't internal or external
> command, runtime program or batch file.
> Registering the new version...
> ""\HelpLibManager.exe"" isn't internal or external
> command, runtime program or batch file.
> Done.
> Press any key for exit...

How can I solve it?
Svetlozar
Telerik team
 answered on 05 Sep 2016
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?