Telerik Forums
JustMock Forum
2 answers
115 views

Hello,

I have the following code, as a test, but it is not working at all.

var timer1 = new Timer(500);
Mock.Arrange(() => new Timer(Arg.IsAny<double>())).Returns(timer1);
var sameTimer = new Timer(200);
Assert.AreEqual(timer1,sameTimer);

 

The use case, is that we have lots of timers in our legacy code that need mocking.  They are not injected so I would like to override the constructor if possible and return a different object.   Is this doable?

Thanks 

Phillip Davis

Phillip
Top achievements
Rank 1
Iron
 answered on 05 Jun 2023
1 answer
104 views

Hello

I generate a syntax tree which I will format with Formatter.Format() from the package Microsoft.CodeAnalysis.CSharp.Workspaces 4.4.0 and .NET 6. A test exists where the formatter is used but when the JustMock profiler is enabled an InvalidProgramException is thrown. When the profiler is disabled everything works fine. It fails on Windows and on Linux.

Exception

  Message: 
System.InvalidProgramException : Common Language Runtime detected an invalid program.

  Stack Trace: 
ContextIntervalTree`2.ctor(TIntrospector& introspector)
FormattingContext.ctor(AbstractFormatEngine engine, TokenStream tokenStream)
AbstractFormatEngine.CreateFormattingContext(TokenStream tokenStream, CancellationToken cancellationToken)
AbstractFormatEngine.Format(CancellationToken cancellationToken)
CSharpSyntaxFormatting.Format(SyntaxNode node, SyntaxFormattingOptions options, IEnumerable`1 formattingRules, SyntaxToken startToken, SyntaxToken endToken, CancellationToken cancellationToken)
AbstractSyntaxFormatting.GetFormattingResult(SyntaxNode node, IEnumerable`1 spans, SyntaxFormattingOptions options, IEnumerable`1 rules, CancellationToken cancellationToken)
Formatter.GetFormattingResult(SyntaxNode node, IEnumerable`1 spans, Workspace workspace, OptionSet options, IEnumerable`1 rules, CancellationToken cancellationToken)
Formatter.Format(SyntaxNode node, IEnumerable`1 spans, Workspace workspace, OptionSet options, IEnumerable`1 rules, CancellationToken cancellationToken)
Formatter.Format(SyntaxNode node, Workspace workspace, OptionSet options, CancellationToken cancellationToken)
UnitTest1.Test1() line 23

Reproduction

You can reproduce this by writing an unit test for that (I used xUnit):

        [Fact]
        public void Test1()
        {
            var classText = @"using System; namespace TestNameSpace.Orders { public class Order
                            {
                                public Guid Id { get; set; }
                            }
                        }";

            var syntaxTree = CSharpSyntaxTree.ParseText(classText);
            var workspace = new AdhocWorkspace();

            var formattedClassText = Formatter.Format(syntaxTree.GetRoot(), workspace).ToFullString();

            var expected = @"using System;
namespace TestNameSpace.Orders
{
    public class Order
    {
        public Guid Id { get; set; }
    }
}";
            Assert.Equal(expected, formattedClassText);
        }
    }

System Info

JustMock

See attachments. We do not use the free edition.

.NET

dotnet --info
.NET SDK:
 Version:   7.0.200
 Commit:    534117727b

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19045
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.200\

Host:
  Version:      7.0.3
  Architecture: x64
  Commit:       0a2bda10e8

.NET SDKs installed:
  6.0.406 [C:\Program Files\dotnet\sdk]
  7.0.200 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download
Ivo
Telerik team
 answered on 09 Mar 2023
1 answer
169 views

I've run into an interesting problem with JustMock. In a nutshell, if you use the C# using declaration (as opposed to a using block), and have JustMock mocking anything in the same assembly, the runtime will throw an InvalidProgramException.

I was able to isolate it down to the narrowest possible case as a proof of point. Using NUnit as the testing framework (I haven't tried with others), and a reference to JustMock, the following code will compile successfully, but at runtime will throw an InvalidOperationException when trying to run the unit test.

[TestFixture]
public class Fixture
{
    public interface ITest { }

    public class TestClass : IDisposable
    {
        public void Dispose()
        {
        }
    }

    [Test]
    public async Task Test()
    {
        ITest mock = Mock.Create<ITest>();
        using TestClass test = new();
    }
}

I've played with various permutations of the problem, and it happen on .NET 4.8 (which we are using) as well as .NET 6.0, and with the version of JustMock we are using (2020.2.616.1) as well as the latest commercial release (2022.3.912.3).

For the problem to occur, the using declaration and the mock creation need not be in the same test, or even in the same fixture, but the test with the using declaration will always fail with the exception. If the async modifier is removed from the unit test, the problem goes away.

Turning off the profiler makes the problem go away, as does turning off the Automatic Mock Repository Cleanup Enabled flag in the JustMock options.

I've attached a simple .NET 6.0 based project that demonstrates the issue.

Ivo
Telerik team
 answered on 12 Oct 2022
1 answer
185 views

I'm working on tests for legacy code that instantiates a ServiceController (in System.ServiceProcess) to check for a running SQL Server instance. I want to add the tests to our CI/CD build pipeline, so I decided to use JustMock to future-mock ServiceController since the service in question will not exist on the build server. These tests are all using xUnit.

However, the test (which has 3 variants specified via xUnit's InlineData attributes) started hanging on the first variant. It's not just long-running; I've let it run for hours (it's normally a 3-second test at most), and CPU usage is negligible. ServiceController is IDisposable, so I figured it might have something to do with that.

After a lot of paring down, I can repro the problem with the following code in a new project:

public class JustMockTest
{
    [Theory]
    [InlineData("test")]
    public void TestMethod(string whatever)
    {
        Mock.Arrange(() => new ServiceController()).DoNothing();
    }
}

However, this test (in the same class) works fine and completes in about 1s - note that it's a fact, not a theory:

[Fact]
public void TestMethod2()
{
    Mock.Arrange(() => new ServiceController()).DoNothing();
}

This is JustMock v2022.1.223.1, xUnit v2.4.1.0, and VS 2022.

At first I wondered if my 3 variants were somehow stepping on each other's Mock.Arrange() statements; but as you can see, the top test, while declared as a theory, only has 1 variant and still has the problem. What's also interesting is that an analogous setup for MSTest also runs fine - so this seems to be xUnit-specific somehow:

[TestClass]
public class UnitTest1
{
    [DataTestMethod]
    [DataRow("test")]
    [DataRow("test2")]
    public void TestMethod1(string whatever)
    {
        Mock.Arrange(() => new ServiceController()).DoNothing();
    }
}

At first I thought IDisposable classes may be the problem, but it seems it's actually MarshalByRefObject; it's a base class of ServiceController and others I can repro with (WebClient, MemoryStream, etc.), and I can also repro by mocking a simple class of my own that extends MarshalByRefObject.

Is this a known issue? Are there recommended workarounds (without having to ditch xUnit for MSTest or another framework)?

Thanks!
Jeremy

Ivo
Telerik team
 answered on 06 Jul 2022
0 answers
145 views

Hi,

I'm interested in Telerik JustMock, so I'm installing and testing a trial version of JustMock.
The worry is that my development environment is disconnected from the Internet, so when I purchase the full version, it cannot be installed.
Are there any problems with the installation and operation of the full version even in an environment where the Internet is disconnected?

Thank you.
CB
Top achievements
Rank 1
 updated question on 08 Apr 2022
0 answers
193 views

I want the Mock to return the SQL statement and Not execute the statement. This is what i have done but it keeps running the "connection.execution(SQL)".  

                         

 

string SQL = "";

            Mock.Arrange(() => connection.Execute(Arg.AnyString, null, null, null, null)).DoInstead((string arg1) => { SQL = arg1; }).IgnoreInstance().Returns(1);



            WebApiProject.Controllers.DeleteController dc = new WebApiProject.Controllers.DeleteController();
           
            dc.Post(mockedDeleteObject);

            Assert.IsTrue(SQL == $"DELETE FROM TEST WHERE 1=1");

 

 

Post{

                string SQL;
                SQL = $"delete from {TableName}  where  {WhereClause} ";

                con.Execute(SQL,parameters );}

 

 

 

But it keeps running the sql executable statement

 

 

 

Ope
Top achievements
Rank 1
 asked on 03 Mar 2022
5 answers
2.1K+ views
01.
[Fact]
02.public void Reconcile_LocationGroupChangedEvent_ReconcileSuccessfully()
03.{
04.    using (var container = new MockingContainer<Assignment>(settings))
05.    {
06.        //Arrange
07.        container.Arrange<IFacade>(x =>x.Initialize()).OccursAtMost(1);
08.                 
09.        //Act
10.        container.Instance.Reconcile();
11. 
12.        //Assert
13.        container.AssertAll();
14.    }
15.}
16. 
17.// Method under test
18. 
19.public void Reconcile()
20.
21.    Task.Factory.StartNew(() => facade.Initialize());
22.}

 

I am getting intermittent errors when running the above Test. It fails so infrequently that I am unable to debug. When it fails, here is what I see in the logs.

Reconcile_Test [FAIL]
System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
Stack Trace:
     at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
     at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Enumerable.<SelectManyIterator>d__23`3.MoveNext()
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at Telerik.JustMock.Core.MocksRepository.GetDebugView(Object mock)
     at Telerik.JustMock.DebugView.<>c.<get_CurrentState>b__1_0()
     at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
     at Telerik.JustMock.DebugView.TraceEvent(IndentLevel traceLevel, Func`1 message)
     at Telerik.JustMock.Core.Context.MockingContext.ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
     at Telerik.JustMock.Helpers.FluentHelper.<>c__DisplayClass13_0`1.<AssertAll>b__0()
     at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal(Action guardedAction)
     at Telerik.JustMock.AutoMock.MockResolver.ForEachMock(Action`1 action)
     at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal(Action guardedAction)
  E:\agt01\COM-CN-JOB1\AssignmentTest.cs(14,0): at AssignmentTest.Reconcile_Test()
Ivo
Telerik team
 answered on 15 Feb 2022
2 answers
711 views

Unfortunately my JustMock does not work anymore. I get this exception:

  Message: 
    Test method ActuIT.Futurama.Test.TestEdit.EditorTest threw exception: 
    Telerik.JustMock.Core.ElevatedMockingException: Cannot mock 'System.IO.File'. The profiler must be enabled to mock, arrange or execute the specified target.

  Stack Trace: 
    ProfilerInterceptor.ThrowElevatedMockingException(MemberInfo member)
    MocksRepository.InterceptStatics(Type type, MockCreationSettings settings, Boolean mockStaticConstructor)
    <>c__DisplayClass69_0.<SetupStatic>b__0()
    ProfilerInterceptor.GuardInternal(Action guardedAction)
    Mock.SetupStatic(Type staticType, Behavior behavior)
    TestEdit.PerformTest(TestInput testInput, String& logFile) line 1413
    TestEdit.PerformTest(String testFile, String& logFile) line 1400
    TestEdit.EditorTest() line 360

Some background:

- I started of with VS2019 version 16.10.4 and JustMock version 2019.2.620.1

- Profiler is enabled in Extensions->JustMock

- tried to reinstall -> no effect

- upgraded VS2019 to 16.11.9 -> no effect

- upgraded to JustMock 2022.1.119.1 -> no effect

- Above is just a sample, all of my unit tests that use JustMock fail

- I checked a colleague and he had exactly the same problem

Any setting/config I might have missed?

Regards,

Dirk

 

 

 

 

 

 

Chris Vrolijk
Top achievements
Rank 1
Iron
 answered on 31 Jan 2022
1 answer
200 views

I added C# JustMock Test Project (.NET Framework) to my solution. The resulting project has five references Microsoft.VisualStudio.TestPlatform.TestFramework, Microsoft.VisualStudio.TestPlatform.TestFrameworkExtensions, System, System.Core and Telerik.JustMock. All of the references had the yellow alert icon next to them. Looking at the properties, none showed a Path.

I was surprised to see System.Core as a reference, since I selected the .NET Framework project. To verify, I removed that project and added a new Test Project, verifying I selected the .NET Framework. Same results.

As a test, I added a VB.NET JustMock Test Project (.NET Framework). It had two erroring references – the Microsoft TestPlatforms. Using NuGet Package Manager, I updated them both from 1.4.0 to 2.2.7. That eliminated the error.

I used NuGet to update the same two references in my C# project, but they still show errors. I also tried removing the reference to Telerik.JustMock.dll and re-adding it by browsing to the file. Still shows error.

Finally, my VB.NET project builds, but my C# build errors with this message: “This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\packages\MSTest.TestAdapter.1.3.2\build”. Doing the package restore does not resolve this message.

Any thoughts as to how I can resolve this? Thanks in advance.

Steve.

Mihail
Telerik team
 answered on 20 Sep 2021
1 answer
526 views
I have a parent report with 
inline CSV1 
Descr Value
Verified 1
Not-Verified 0

inline CSV2
Descr value
ChartView 0
GridView 1

Report Parameters
Id supplied to report
Verified visible selected from inline CSV1
View visible selected from inline CSV2
Months visible multi-selected from Distinct list of 'FormattedDate' in 'dsMain'
Statuses visible multi-selected from Distinct list of 'Status' in 'dsMain'


I have a stored proc taking params 'Id' and 'Verified' from parent report to populate 'dsMain' datasource.
Stored proc to be called only when 'Id' or 'Verified' change and returning data including.... 
Id
Verified
Date(01-mm-yyyy) 
FormattedDate(MMM yy)
Status (S1,S2,S3,S4,S5)
DataItem1
DataItem2
DataItem3
DataItem4
DataItem5.... etc

I have a sub-report 'Chart' displaying a clustered vertical barchart
'Chart' sub-report is only visible if 'View' parameter of parent report is value of ChartView(0)
'Chart' sub-report to use 'dsMain' from parent report and filtering on 'Months' and 'Statuses' and sorting by 'Date'
'Chart' is grouped on 'FormattedDate' and each series is derived for each value in 'Status', value is Count('Status')

I have a sub-report 'Grid' displaying a datagrid
'Grid' sub-report is only visible if 'View' parameter of parent report is value of GridView(1)
'Grid' sub-report to use 'dsMain' from parent report and filtering on 'Months' and 'Statuses' and sorting by 'Date'

I cannot work out how to access the data source 'dsMain' from the sub-reports.
I cannot work out how to stop the stored proc from being called whenever 'View', 'Months' or 'Status' gets changed by the user.
The key issue here is the cost of running the Stored Proc when it has all the necessary data to filter and populate the controls on either of the sub reports.
The only time the data source should be refreshed is if the 'Id' or 'Verified' parameters of the parent report is changed.
Neli
Telerik team
 answered on 13 Aug 2021
Narrow your results
Selected tags
Tags
+? more
Top users last month
horváth
Top achievements
Rank 2
Iron
Iron
Steve
Top achievements
Rank 2
Iron
Erkki
Top achievements
Rank 1
Iron
Mark
Top achievements
Rank 2
Iron
Iron
Veteran
Jakub
Top achievements
Rank 1
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
horváth
Top achievements
Rank 2
Iron
Iron
Steve
Top achievements
Rank 2
Iron
Erkki
Top achievements
Rank 1
Iron
Mark
Top achievements
Rank 2
Iron
Iron
Veteran
Jakub
Top achievements
Rank 1
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?