Telerik Forums
JustMock Forum
2 answers
282 views

Hi,

  I have a MessageBox.Show method, which has a CallBack action associated with it. I want to Pass a Mocked parameter to that action, which would eventually execute the action statements based on Parameter passed.

Action<DialogResult> callback = (result) =>
                    {
                        if (result == DialogResult.Yes)
                        {
                           //Do something here  
                        }
                    };
MessageBox.Show("Are you Sure?","Test App",DialogButton.YesNo, DialogImage.Question,callback);

  I tried something like below, but it did not work.

 

Mock.Arrange(() => dialog.Show(Arg.AnyString, Arg.AnyString,DialogButton.YesNo, DialogImage.Question, Arg.IsAny<Action<DialogResult>>())).
           DoInstead((Action<DialogResult> action) =>
           {
             action.Invoke(DialogResult.Yes);
           });

 I want to execute the Original callback, with a mocked DialogResult. 

How can we do that?

 

Thanks in Advance,

Sagar

 

Sagar
Top achievements
Rank 1
 answered on 17 Aug 2015
11 answers
1.1K+ views

Hello guys!

 

I get a Telerik.JustMock.Core.ElevatedMockingException when I run: Mock.Arrange(() => _client.GetAsync(Arg.IsAny<string>())).Returns(ShowMeTheMoney());

static async Task<HttpResponseMessage> ShowMeTheMoney()
        {
            HttpRequestMessage httpRequest = new HttpRequestMessage();
            httpRequest.SetConfiguration(new HttpConfiguration());
            HttpResponseMessage response = httpRequest.CreateResponse(HttpStatusCode.OK, new List<SmsDTO>() { new SmsDTO()});
            return response;
        } 

    [TestMethod]
        public void GetAll_NullResultTes3t()
        {
            //Assert.IsTrue(Mock.IsProfilerEnabled);
            _client = Mock.Create<HttpClient>();
            _controller = new SmsController(new Persistence(_client));
            _controller.Request = new HttpRequestMessage();           
            Mock.Arrange(() => _client.GetAsync(Arg.IsAny<string>())).Returns(ShowMeTheMoney());
            IEnumerable<SmsDTO> actualResult = _controller.GetAll();
            CollectionAssert.AllItemsAreNotNull(actualResult.ToList());
        }

 

I use VS2015 Enterprise and I have disabled IntelliTrace(Tools>Options>IntelliTrace: Uncheck the Enable IntelliTrace checkbox). Also I enabled the profiler from the JustMock Menu.

I get the following in Event logs:
Info: .NET Runtime version 4.0.30319.0 - The profiler has requested that the CLR instance not load the profiler into this process.  Profiler CLSID: '{9999995d-2cbb-4893-be09-fce80abc7564}'.  Process ID (decimal): 7808.  Message ID: [0x2516].

Info: TraceLog Profiler component initialized successfully, process vstest.executionengine.x86.exe

Info: .NET Runtime version 4.0.30319.0 - The profiler was loaded successfully.  Profiler CLSID: '{9999995d-2cbb-4893-be09-fce80abc7564}'.  Process ID (decimal): 7724.  Message ID: [0x2507].

Error:

The description for Event ID 0 from source Application cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

Error Handler Exception: System.ServiceModel.CommunicationException: There was an error reading from the pipe: The pipe has been ended. (109, 0x6d). ---> System.IO.IOException: The read operation failed, see inner exception. ---> System.ServiceModel.CommunicationException: There was an error reading from the pipe: The pipe has been ended. (109, 0x6d). ---> System.IO.PipeException: There was an error reading from the pipe: The pipe has been ended. (109, 0x6d).
   at System.ServiceModel.Channels.PipeConnection.OnAsyncReadComplete(Boolean haveResult, Int32 error, Int32 numBytes)
   --- End of inner exception stack trace ---
   at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ConnectionStream.EndRead(IAsyncResult asyncResult)
   at System.Net.FixedSizeReader.ReadCallback(IAsyncResult transportResult)
   --- End of inner exception stack trace ---
   at System.Net.Security.NegotiateStream.EndRead(IAsyncResult asyncResult)
   at System.ServiceModel.Channels.StreamConnection.EndRead()
   --- End of inner exception stack trace ---
   at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.EndTryReceive(IAsyncResult result, Message& message)
   at System.ServiceModel.Dispatcher.DuplexChannelBinder.EndTryReceive(IAsyncResult result, RequestContext& requestContext)
   at System.ServiceModel.Dispatcher.ErrorHandlingReceiver.EndTryReceive(IAsyncResult result, RequestContext& requestContext) \r\n    at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.EndTryReceive(IAsyncResult result, Message& message)
   at System.ServiceModel.Dispatcher.DuplexChannelBinder.EndTryReceive(IAsyncResult result, RequestContext& requestContext)
   at System.ServiceModel.Dispatcher.ErrorHandlingReceiver.EndTryReceive(IAsyncResult result, RequestContext& requestContext)

the message resource is present but the message is not found in the string/message table

Info: .NET Runtime version 4.0.30319.0 - The profiler has requested that the CLR instance not load the profiler into this process.  Profiler CLSID: '{b7abe522-a68f-44f2-925b-81e7488e9ec0}'.  Process ID (decimal): 6696.  Message ID: [0x2516].

 According to regedit, CLSID: '{9999995d-2cbb-4893-be09-fce80abc7564} corresponds to IntelliTrace and  CLSID: '{b7abe522-a68f-44f2-925b-81e7488e9ec0}' to CodeWeaverProfiler class. 

 Could you please help?

 

Thanks!

Ciprian
Top achievements
Rank 1
 answered on 12 Aug 2015
5 answers
147 views

Let's say I have a class

 

class MyClass
{
    public virtual void MyFunc1( DateTime somedate)
    {}

    public virtual void MyFunc2( )
    {
       MyFunc1(SomeObject.SentDate)

    }

}

where SomeObject has a field called SentDate.

SomeObject is in a global scope and, for some reasons, I can't provide a value for it so will always be null when mocking.

I can arrange for MyFunc1() to do whatever I want but, when I call MyFunc2(), it will try to evaluate its parameter SomeOject.SentDate and, because SomeObject is null, this will fail with an exception.

Is there a way to skip the evaluation of SomeObject.SentDate parameter?

Stefan
Telerik team
 answered on 12 Aug 2015
1 answer
119 views

I have a private Generic method and I want to call it using Private Accessor, How can i do that.

private void PopulateCollection<T>(List<IDataItem> fromCollection, ObservableCollection<T> toCollection){}

 

Thanks

Vikas

Stefan
Telerik team
 answered on 11 Aug 2015
5 answers
139 views

 I have this code:

<p>using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.JustMock;
using Telerik.JustMock.Core;
  
  namespace TestMock1
{
    [TestClass] public class UnitTest1 {
         
[TestMethod]
        public void TestMethod1()
        { var mock = Mock.Create<Cls1>(); Mock.Arrange(() => mock.Func1()).Returns(2);
              mock.Func2();
        }
    }
  public class Cls1 { public Cls1()
        {
  
        }
  internal virtual int Func1()
        { int x = 1; return x;
        }
  internal virtual int Func2()
        { return Func1();
        }
  
    }
  
  
}​</p>

Florian
Top achievements
Rank 1
 answered on 07 Aug 2015
1 answer
80 views

Hello,

I am trying to do Mock.Arrange() in the following manner:

 var practice = Mock.Create<PracticeOptions>();
 Mock.Arrange(() => practice.PolicyExpirationDays).Returns(30);

I am getting the exception in the second line saying that "Cannot mock 'Int32 get_PolicyExpirationDays()'. The profiler must be enabled to mock, arrange or execute the specified target."

I tried updating\removing\installing JustMock on my machine but nothing is working out.

Please look into this asap as it's a bit urgent.

Thanks,

Abdeali

​

Kaloyan
Telerik team
 answered on 06 Aug 2015
6 answers
483 views

If given the following code, is it possible to mock the Base class constructor without mocking the derived class constructor so that MyProp gets set?:

    public class Base
    {
        public Base()
        {
            throw new NotImplementedException();
        }

        public string MyProp { get; set; }
    }

    public class Derived : Base
    {
        public Derived()
        {
            MyProp = "hello";
        }
    }

Stefan
Telerik team
 answered on 21 Jul 2015
1 answer
166 views

My team has been using Microsoft Fakes for a while, but recently looked into JustMock and really liked what we saw and wanted to use JustMock going forward and slowly replace all MS Fakes uses to JustMock.  After a bit of hassle getting our Build Server to handle JustMock appropriately, it seems that none of our tests that used MS Fakes now work, getting the error: Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException: Failed to get address of function SetDetourProvider from library 'C:\Program Files (x86)\Telerik\JustMock\Libraries\CodeWeaver\32\Telerik.CodeWeaver.Profiler.dll'.

This is a big problem for us as we have too many tests using Fakes to convert them all at once and can't afford an all or nothing approach here.  Is there a way to get them both working on the same Build Server?  Each solution/build only uses one framework...

Thanks!

Stefan
Telerik team
 answered on 21 Jul 2015
3 answers
397 views

I'm trying to understand if JustMock and Microsoft Fakes both have a place in the same project as complementary tools, or if it's a case of choosing one versus the other.

thanks

Stefan
Telerik team
 answered on 21 Jul 2015
3 answers
142 views

Hi, I am testing a method whereby a value is set in the method. Initially I set the return value using JustMock. This is SharePoint by the way.

01.// arrange
02.SPItemEventProperties properties = Mock.Create<SPItemEventProperties>(Behavior.Strict);
03.Mock.Arrange(() => properties.BeforeProperties["Title"]).Returns("Old Title");
04.Mock.Arrange(() => properties.AfterProperties["Title"]).Returns("New Title");
05. 
06.// act
07.Person p = new Person();
08.p.DoSomething(properties);     // -> properties.AfterProperties["Title"] = "abc";
09. 
10.// assert
11.Assert.AreEqual("abc", properties.AfterProperties["Title"].ToString()); // fails here since AfterProperties is still "New Title"

I realize that the .Return is causing the same value to be returned. How can I check that the correct value is being set inside the method / can I get the value which has been set in the method back in the assert.arequal?

 

Thanks,

Gary.

 

Stefan
Telerik team
 answered on 30 Jun 2015
Narrow your results
Selected tags
Tags
+? more
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?