Considering the sample class
using Azure.Messaging.ServiceBus;
public class Class2
{
private static ServiceBusSender messageToTopicSender;
private static string topicName;
public static void SetRequestTopicClient(string serviceBusConnectionString, string topName)
{
topicName = topName;
ServiceBusClient serviceBusClient = new ServiceBusClient(serviceBusConnectionString);
messageToTopicSender = serviceBusClient.CreateSender(topicName);
}
}
and the following tests
[TestClass]
public class Class1Test
{
[TestInitialize]
public void SetUp()
{
Mock.Arrange(() => Class2.SetRequestTopicClient("", "")).DoNothing();
}
[TestMethod]
public void TestA()
{
Class2.SetRequestTopicClient("", "");
}
}
[TestClass]
public class Class2Test
{
static ServiceBusClient serviceBusClient = Mock.Create<ServiceBusClient>();
ServiceBusSender messageToTopicSender = Mock.Create<ServiceBusSender>();
[TestInitialize]
public void SetUp()
{
Mock.Arrange(() => new ServiceBusClient("conStr")).Returns(serviceBusClient);
Mock.Arrange(() => serviceBusClient.CreateSender("myTopic")).Returns(messageToTopicSender);
}
[TestMethod]
public void TestB()
{
Class2.SetRequestTopicClient("conStr", "myTopic");
}
}
Executing the tests in order TestA -> TestB results in failed TestB, but changing the order or runining them standalone succeeds.The outcome of the tests should not be dependent of the execution order.
Considering the following simple scenario:
public class Class1
{
private string arg1;
public Class1(string arg1)
{
this.arg1 = arg1;
}
public Class3 Method(string arg)
{
throw new NotImplementedException();
}
}
public class Class2
{
static Class3 class3Instance;
public static void StaticMethod(string arg1, string arg2)
{
Class1 class1Instance = new Class1(arg1);
class3Instance = class1Instance.Method(arg2);
}
}
public class Class3
{
}
[TestClass]
public class Class2Test
{
static Class1 mockClass1 = Mock.Create<Class1>();
Class3 mockClass3 = Mock.Create<Class3>();
[TestInitialize]
public void SetUp()
{
Mock.Arrange(() => new Class1("arg1")).Returns(mockClass1);
Mock.Arrange(() => mockClass1.Method("arg2")).Returns(mockClass3);
}
[TestMethod]
public void TestB()
{
Class2.StaticMethod("arg1", "arg2");
}
}
The test passes, but the debug trace output contains the warning message: "Calling one test method from another could result in unexpected behavior and must be avoided. Extract common mocking logic to a non-test method." even there is no call to the other test methods.
I've already created an issue in the JustMock repo, you can find it here: https://github.com/telerik/JustMockLite/issues/162. Below is the text from that issue...
I'm currently working on a PR to add JustMock to the suite of performance tests that currently exist in that repository. The problem is that some of the tests (like OneParameter and Callback) take a long time to finish for JustMock. I'm not sure why, but it's making it such that JustMock can't be added to the test suite. More to the point, the PR can't be accepted as-is because it would take too long to finish.
One thing Steve (owner of the repo) and I talked about is if the invocationCount value passed to the SimpleJobAttribute (which exists on both the MockingBenchmark and MockingBenchmark<T> classes) is reduced from 100,000 to something like 1,000, the tests then finish, but that doesn't address the underlying issue why these tests for JustMock slow down over repeated called.
There may be something in the benchmark tests with the way I've set things up with JustMock that may be incorrect. I've never used JustMock until today :). If there's something that I'm doing right, please let me know.
The following test is not working anymore since version JustMock_2021_2_615_4 but was working with JustMock_2021_1_326_1 and older versions:
public interface ITest
{
void Test(string text);
}
[TestMethod]
public void MyTestMethod()
{
var mock = Mock.Create<ITest>();
Mock.Arrange(() => mock.Test("")).IgnoreArguments().OccursNever();
Mock.Arrange(() => mock.Test(Arg.Matches<string>(a => a == "a"))).InOrder().OccursOnce();
Mock.Arrange(() => mock.Test(Arg.Matches<string>(a => a == "b"))).InOrder().OccursOnce();
mock.Test("a");
mock.Test("b");
Mock.Assert(mock);
}
If I remove the OccorsNever line, its working.
If I remove the 2 InOrder, its working.
Seems that something change there: https://www.telerik.com/support/whats-new/justmock/release-history/justmock-r2-sp1-2021
Allow the developer to create custom behaviors and use them in an arrangement. Something like the following:
Mock.Arrange(() => foo.CalcData(Arg.AnyInt, Arg.AnyInt), new IBehavior[]
{
new LogInvocation(),
new ReturnBaseOrDefaultValue(),
});
The new public API should allow the developer to access the invocations for a particular arrangement. Something like the following:
IEnumerable<IInvocaiton> invocations = foo.GetInvocationsFor((x) => x.CalcData(2, 2));
Implement a new public API that will allow the developer to iterate over all invocations of mock arrangements. Something like the following:
IEnumerable<IInvocaiton> invocations = Mock.Invocations;
I filed a prior ThreadLeak:
Thread leak in JustMock (telerik.com)
I think I've found another one: (VS version 16.10.31321.278)
telerik_justmock_debugwindow_service_client!Telerik.JustMock.DebugWindow.Service.Client.ServiceHostMonitor.MonitorTimer_Elapsed
Apparently, the MonitorTimer_Elapsed code is running on a threadpool thread and it takes longer than 1 sec (perhaps it's waiting on a lock?).
Another timer tick occurs and the method is called again. Eventually, the ThreadPool runs out of threads and grows by about 1 thread per second (1Mb /sec leak)
In one dump 2Gig was used (1962 threads on the same MonitorTimer_Elapsed stack frame)
In that same dump there were 52 stacks with this frame:
telerik_justmock_debugwindow_service_client!Telerik.JustMock.DebugWindow.Service.Client.ConnectionSetupServiceClientBase`[[System.__Canon,mscorlib]].get_ServiceAvailable
(this was the same item as my prior report).
The documentation on Fluent Mocking ends with this statement:
Important
Note that when you use Fluent Asserts only arrangements marked with either MustBeCalled or Occurs will be verified. For other tests you have to use the explicit assert.
What this fails to note is that, while this is true of the function "Assert", there is another function, "AssertAll", which will flag an error if any Arranged function call was not utilized.
On a related note, I left other suggestions for this same page a day or two ago. I would have liked to leave the above statement using the same feedback utility, but I can no longer find the control that I used to leave those initial suggestions.
The following test fails:
[TestMethod]
public void TestCallOrderWithInOrder()
{
// Arrange
var mockClass1 = Mock.Create<Class1>();
var mockClass2 = Mock.Create<Class2>();
Mock.Arrange(() => mockClass1.Method1())
.InOrder();
Mock.Arrange(() => mockClass2.Method1())
.InOrder()
.Occurs(2);
Mock.Arrange(() => mockClass1.Method2())
.InOrder();
// Act
mockClass1.Method1();
mockClass2.Method1();
mockClass2.Method1(); // <--- this call is not allowed by
// InOrder clause currently
// and throws AssertFailedException
mockClass1.Method2();
// Assert
Mock.Assert(mockClass1);
Mock.Assert(mockClass2);
}