Telerik Forums
JustMock Forum
5 answers
138 views

I have not used JustMock in the past year or so but I used to use it to do future mocking of objects that are created in the code.  But now I cannot find any examples of this in my own code or online, so I am not sure how to do this. But I have done it before so I know it is possible.  So I'm hoping someone can point me in the right direction here.

The code below shows what I am doing.  I setup a mock of my class that I am testing, but as a PrivateAccessor so I can use that as needed.  So I grab the instance as my sut. There there, when I run into code that creates another object and does not pass that object in using DI, I thought that you were able to mock that with a .Returns() so that it returns a fake version of whatever object you typically return. However, I cannot get the context to work with the arrange object and I cannot figure out what I need to do.  I did try adding the arrange to the sut, but I could not figure out the syntax.

The first two lines... the mock and sut work fine.  Then the next two lines also work fine, the Mock.Arrage and my test below it to get LoanData.  But now when my sut runs and does things and encounters code such as var something = new LoanData() it is not using the arrange, but the actual code.

So what am I missing?  How can I get that Mock.Arrange to apply to the code from within the Sut?  I can do Sut.Array() but I cannot figure out the syntax. 

Any suggtions are appreciated.  I'm sure it is just a syntax issue and I don't see how to set it up.

public PricingBase()
{
           var mock = new PrivateAccessor(new Classes.PricingEngine.Refactor.PricingEngine(new PricingEngineService()));
           Sut = (Classes.PricingEngine.Refactor.PricingEngine)mock.Instance;
            
           Mock.Arrange(() => new LoanData()).IgnoreArguments().Returns(CreateFakeLoanData());
           var test = new LoanData();
       }

 

 

Kamen Ivanov
Telerik team
 answered on 24 Apr 2017
4 answers
167 views

Hello,

I see from answers to two old posts to this forum (URLs below) that a license for each build server or agent was not necessary as recently as March 2012, but that was quite some time ago.  Could someone from Telerik confirm that the same licensing policy remains in effect?  And if not, whether I need to acquire licenses per server or per build agent (we have some build servers running multiple agents), and whether the licensing mechanism plays well with VMs (all of our build servers are VMs).  Thanks in advance.

http://www.telerik.com/forums/do-you-need-to-install-on-the-build-server

http://www.telerik.com/forums/justmock-licensing-tfs-2010

Also, perhaps it would be worthwhile to have licensing questions like this in some FAQs area of the main site.

 

Tom Slavens,

Configuration Architect, Platform Architecture, Software Services

Veterans United Home Loans

 

Oliver
Top achievements
Rank 1
 answered on 18 Apr 2017
2 answers
237 views

I'm having an issue with running xUnit with JustMock and dotCover on TeamCity.

I've setup my environment parameter JUSTMOCK_INSTANCE=1 on the build configuration and linked the dotCover profiler on the "Telerik JustMock Configuration" interface. 

I'm using the "xUnit.net + dotCover 💕" metarunner and it is executing the tests. But after it has executed the tests (no errors), dotCover does not finish generating the coverage data file. The process just "hangs", so to speak, after the last test.

When I unlink the dotCover profiler on the "Telerik JustMock Configuration" interface, and use JustMockRunner.exe, the process does finish, but no coverage data is generated. 

TeamCity Command Line script (in stead of meta runner):

%teamcity.tool.dotCover%\dotcover cover /TargetExecutable="%just_mock_Path%\JustMockRunner.exe" /TargetArguments="%system.teamcity.build.workingDir%\packages\xunit.runner.console.2.2.0\tools\xunit.console.exe mytestassembly.dll" /ReturnTargetExitCode

 

When running xUnit directly is does not execute most of the unit tests, because JustMock is not in elevated mode of course, but it does finish the process generate the coverage data. Indicating for me, that dotCover is working fine.

TeamCity command line script (in stead of meta runner):

%teamcity.tool.dotCover%\dotcover cover /TargetExecutable="%system.teamcity.build.workingDir%\packages\xunit.runner.console.2.2.0\tools\xunit.console.exe" /TargetArguments="mytestassembly.dll" /ReturnTargetExitCode

 

It makes sense that the dotCover profiler should run together with JustMock so the data can be generated, but every time I link the profiler, the process hangs after all tests have been executed. 

Different test assemblies show the same result.

Versions:

JustMock: 2016.3.914.2

Team City 10.0.2 (build 42234)

xUnit: 2.2.0

dotCover: 2016.2.2

 

Is this a known issue? Please let me know if you need more information.

Thanks.

Kamen Ivanov
Telerik team
 answered on 10 Apr 2017
1 answer
212 views

I have to implement a unit test method that needs to mock datacontextcontainer. Below is the code i have to mock

using (var dcc = new DataEntities.DataContextContainer())            

{

}

How can i mock DataEntities.DataContextContainer(). When ever i use to call this statement i m getting new instance. Can some please help me on this.

Kamen Ivanov
Telerik team
 answered on 09 Mar 2017
6 answers
804 views

Hello,

I'm new to JustMock and have been playing with it only for a few hours.  I have been able to convert a few of my existing tests from Microsoft Fakes to JustMock.  However, there's one test, where I'm having a lot of trouble and I'm hoping someone can help me with it.

 

I have a class that derives from the System.Web.Http.ApiController and here is a snapshot of it:

public class MyApiController : ApiController {
    public HttpResponseMessage Post(EPAQuestionsetLookupRequest lookup) {
        try {
            if (!ModelState.IsValid) {
                return new HttpResponseMessage(HttpStatusCode.NotAcceptable);
            }
            return new HttpResponseMessage(HttpStatusCode.OK);
        } catch {
            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }
    }
}

 

I have a test written as the follows:

[TestMethod]
public void PostNotValidState() {
    var controller = Mock.Create<MyApiController>();
    Mock.Arrange(() => controller.ModelState.IsValid).Returns(false).MustBeCalled();
    // Mock.Arrange(() => ((ApiController)controller).ModelState.IsValid).Returns(false).MustBeCalled();
    var response = controller.Post(lookup);
 
    Assert.AreEqual(HttpStatusCode.NotAcceptable, response.StatusCode);
    Mock.Assert(controller);
}

 

I've tried it in the above way and also using a Container instead.  But, in both cases, it seems like the ModelState.IsValid is continuing to return true (or the method during the test, is actually not using the arrangement I have, which is confirmed by the fact that the Mock.Assert is failing on the MustBeCalled) and is failing my test.  What's the best way for me to be able to mock the ModelState.IsValid to return false and for it to be honored?

Svetlozar
Telerik team
 answered on 23 Feb 2017
2 answers
764 views

There is a private variable that in a static class:

public static class Common     {   

        private static EMailSetting _mailSetting;

......

        public void BeTest()

        {

                if(_mailSetting=null){

                   //do something

               }

               ........

        }

}

How can I  mock "_mailSetting"?

I try this code, It's not work:

Mock.SetupStatic(typeof(Common), Behavior.CallOriginal, StaticConstructor.Mocked);             

Mock.NonPublic.Arrange<EMailSetting>(typeof(Common), "_mailSetting")                 

    .Returns(() =>                 {                    

            var mockMailSetting = Mock.Create<EMailSetting>();                     

            mockMailSetting.mailFrom = "C";                     

            mockMailSetting.mailTo = "D";                

            return mockMailSetting;                 

});

Common.BeTest();

Dino
Top achievements
Rank 1
 answered on 16 Feb 2017
1 answer
271 views
I get how to Mock the Begin method in the pattern, but I cannot find any documentation that shows how to mock the End method or how to handle the Callback. I would love to see an example of how to do this.
Mike
Top achievements
Rank 1
 answered on 15 Feb 2017
1 answer
127 views
I'm trying to use Mock.Assert for a void method but i'm facing an exception "occurrence expectation failed. expected at least 1 call. calls so far: 0". Could anyone please help me out on the same.
Svetlozar
Telerik team
 answered on 10 Feb 2017
3 answers
160 views

I'm using Visual Studio 2015 Update 3.  I installed JustMock and JustCode using Telerik Control Panel.  I created a project using the C# JustMock template.  Both JustMock and JustCode are working, except that I'm not seeing the Create Mock or Arrange Mock menus in JustCode as described here.  Neither the visual aid or or the JustCode menu shows the menus.  This is happening on two different computers.  Any ideas?

Thanks,

Joel

Svetlozar
Telerik team
 answered on 23 Jan 2017
3 answers
130 views

It looks like having the JustMock profiler enabled when attempting to work on .NET Core projects causes some kind of issue that makes it impossible to compile or run the code.

I had the exact same issue detailed here: http://www.lpaneque.com/2016/05/justmock-and-failed-to-initialize-coreclr.html and disabled the profiler (just like the author) and I was able to successfully develop .NET core apps after.

Svetlozar
Telerik team
 answered on 04 Jan 2017
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?