ItemCollection itemCollection_Single = Mock.Create<ItemCollection>(Behavior.Strict);
Mock.Arrange(() => itemCollection_Single.Count).Returns(2).Occurs(3, nameof(itemCollection_Single.Count));
RadComboBox radComboBox_Single = Mock.Create<RadComboBox>(Behavior.Strict);
Mock.Arrange(() => radComboBox_Single.Items).Returns(itemCollection_Single).Occurs(3, nameof(radComboBox_Single.Items));
mockSelectionChangedEventArgs = Mock.Create<SelectionChangedEventArgs>(Behavior.Strict);
Mock.Arrange(() => mockSelectionChangedEventArgs.AddedItems).Returns(new List<ParameterValue>() { selectionRuleRow.AllowedValues[0] }).Occurs(2, nameof(mockSelectionChangedEventArgs.AddedItems));
Mock.Arrange(() => mockSelectionChangedEventArgs.OriginalSource).Returns(radComboBox_Single).Occurs(1, nameof(mockSelectionChangedEventArgs.OriginalSource));
selectionRuleRow.MultipleEnumSelectionChanged.Execute(mockSelectionChangedEventArgs);
Assert.AreEqual(expected: "Yes", actual: selectionRuleRow.Value);
Assert.IsTrue(selectionRuleRow.CanApply);
Mock.Assert(mockSelectionChangedEventArgs); // ==> fails because of set expectations on itemCollection_Single and radComboBox_Single
//Mock.Assert(() => mockSelectionChangedEventArgs.AddedItems, Occurs.Exactly(1));
//Mock.Assert(() => mockSelectionChangedEventArgs.OriginalSource, Occurs.Exactly(1));
For the sake of brevity, I've included a minimal example below. An async event MyEventAsync is defined in class MyClass. An event handler OnMyEventAsync is also defined.
public class MyClass
{
// Event.
public event Func<EventArgs, Task>? MyEventAsync;
}
// Event handler.
private Task OnMyEventAsync(EventArgs args) => Task.CompletedTask;
I then create a mock of MyClass and register the event handler OnMyEventAsync with the event MyEventAsync. Then Raise the event with the syntax given in the documentation.
// Create mock.
var mockClass = Mock.Create<MyClass>(Behavior.Strict);
// Register event handler.
mockClass.MyEventAsync += OnMyEventAsync;
// Raise event on mock.
Mock.Raise(() => mockClass.MyEventAsync += null, EventArgs.Empty);
But I receive the following error:
Telerik.JustMock.Core.MockException : Event signature System.Threading.Tasks.Task OnMyEventAsync(System.EventArgs) is incompatible with argument types (Castle.Proxies.ExternalMockMixinProxy, System.EventArgs)
I've probably missed something very obvious here. I know the above syntax should work with an event of type EventHandler<EventArgs>, but I'm not sure how to modify it to work with event of type Func<EventArgs, Task>. If anyone could clear up my confusion, I'd be most grateful. Thanks! =)
Support,
I am attempting to use JustMock to mock ServiceSecurityContext based on this article here.
If I am understanding this Telerik article, I should be able to use the NuGet package and configure a profiler using a ".runsettings" file without needing to install the configuration tool.
Here is the .runsettings file I have added to my test project:
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<JUSTMOCK_INSTANCE>1</JUSTMOCK_INSTANCE>
<CORECLR_ENABLE_PROFILING>1</CORECLR_ENABLE_PROFILING>
<CORECLR_PROFILER>{B7ABE522-A68F-44F2-925B-81E7488E9EC0}</CORECLR_PROFILER>
<CORECLR_PROFILER_PATH>..\packages\justmock.commercial\2024.2.514.325\runtimes\win-x64\native\Telerik.CodeWeaver.Profiler.dll</CORECLR_PROFILER_PATH>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>
Unfortunately, this did not work and I am still getting the following error when running my tests.
Telerik.JustMock.Core.ElevatedMockingException: Cannot mock 'System.ServiceModel.ServiceSecurityContext'. The profiler must be enabled to mock, arrange or execute the specified target.
Detected active third-party profilers:
* {865fc36f-7fab-44b6-bbb7-c2baea8d9ad2} (from process environment)
Disable the profilers or link them from the JustMock configuration utility. Restart the test runner and, if necessary, Visual Studio after linking.
What am I missing to get this configured?
Thanks,
-Scott
I have a class that overrides the ToString() method and returns part of a value thats set in the constructor of the class. When attempting to mock this class, JustMock ObjectValue calls the ToString() method before the UserInfo class constructor has run. In this case a null ref exception is thrown.
public string FullName => StringHelper.Format("User: '{0}'.", this.User.FullName);
public UserInfo(User author)
{
this.User = author
}
public override string ToString()
{
return this.FullName;
}
public ObjectValue(Tuple<Type, object> objectDesc)
{
AssemblyQualifiedName = objectDesc.Item1.AssemblyQualifiedName;
if (objectDesc.Item2 != null)
{
if (objectDesc.Item1.IsValueType)
{
StringValue = string.Format((objectDesc.Item1 == typeof(char)) ? "'{0}'" : "{0}", objectDesc.Item2);
}
else
{
StringValue = string.Format((objectDesc.Item1 == typeof(string)) ? "\"{0}\"" : "{{{0}}}", objectDesc.Item2);
}
}
else
{
StringValue = "<null>";
}
}
//Arrange
this.userInfo= Mock.Create<UserInfo>(user);
Mock.Arrange(() => this.userInfo.ToString()).Returns("FullName");
In my test I am using both MockingContainer and Mock to do Automocking and create concrete mocks respectively.
At the end of my test I want to ensure all of the functions I setup via Arrange for both the MockingContainer and mocked classes were ran.
Do I need to call .AssertAll() for each mocked object and the MockingContainer or can I simply call Mock.AssertAll() to make sure all Arranged functions were executed? See example below for both approaches.
// Establish mocks
var mockedControllerContainer = new MockingContainer<SomeController>();
var mockedClass = Mock.Create<SomeClass>();
// Establish test variables
IEnumerable<SomeDomainModel> records = new List<SomeDomainModel>();
// Arrange Mocks
mockedControllerContainer
.Arrange<Repository>(repo => repo.FetchRecordsWithName("name"))
.Returns(records);
mockedClass
.Arrange(c => c.SomeFunction())
.Returns(null);
// Assert
mockedControllerContainer.AssertAll();
mockedClass.AssertAll();
// Or can I use the example below to cover all cases?
Mock.AssertAll();
Which approach is best practice to ensure each function was called? This example only had one concrete class but other tests can have many so if I can avoid having to use .AssertAll() for each of them individutally that would be ideal.
The API documentation for Mock.AssertAll() states "Asserts all expected setups in the current context". How is this context defined?
Thank you!
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
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.
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
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);
}
}
See attachments. We do not use the free edition.
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
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.
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