Mock.SetupStatic(
typeof
(DataAccess), StaticConstructor.Mocked);
Mock.Arrange(() => DataAccess.IsRequestAuthenticate(
null
))
.IgnoreArguments().IgnoreInstance()
.Returns(
true
)
.OccursOnce();
Mock.Assert(() => DataAccess.IsRequestAuthenticate(
null
));
7 Answers, 1 is accepted
Thank you for contacting our support.
To answer your question, I'll use the following system under test for example:
static
class
DataAccess
{
public
static
bool
IsRequestAuthenticate(
string
str)
{
throw
new
NotImplementedException();
}
}
To assert occurrence against a certain static arrangement, you can choose one of the following approaches:
In the first example, we will arrange that a specific static method should occur once and in the same time return true, no matter its arguments. Then we will act by calling the
IsRequestAuthenticate
method with "test" as an argument. To assert the occurrence, we need to use Mock.Assert. However, as we want to check for all method calls, we will also need ignore the method arguments in the assert:[TestMethod]
public
void
TestMethod1()
{
// Arrange
Mock.Arrange(() => DataAccess.IsRequestAuthenticate(
null
))
.IgnoreArguments()
.Returns(
true
)
.OccursOnce();
// Act
var actual = DataAccess.IsRequestAuthenticate(
"test"
);
// Assert
Mock.Assert(() => DataAccess.IsRequestAuthenticate(
null
), Args.Ignore());
}
If the above Args.Ignore() is not specified, the test will fail. This is so, because the assert will expect that the
IsRequestAuthenticate
method is called once with null as an argument. This is shown in the next example in which you assert only against an argument specific call:[TestMethod]
public
void
TestMethod2()
{
// Arrange
Mock.Arrange(() => DataAccess.IsRequestAuthenticate(
null
))
.IgnoreArguments()
.Returns(
true
)
.OccursOnce();
// Act
var actual = DataAccess.IsRequestAuthenticate(
null
);
// Assert
Mock.Assert(() => DataAccess.IsRequestAuthenticate(
null
));
}
Further, I removed the IgnoreInstance() from you arrangement, as it is not needed. This is so, because you are arranging against static member.
I hope the above helps. Let me know if there is more I can help you with.
Regards,
Kaloyan
Telerik

Mock.Arrange(() => DataAccess.UpdateUserStatus(
null
))
.IgnoreArguments().IgnoreInstance()
.Returns(1)
.OccursNever();
Mock.Assert(() => DataAccess.UpdateUserStatus(
null
), Args.Ignore());

Mock.Assert(() => DataAccess.UpdateUserStatus(
null
), Args.Ignore(), Occurs.Never());

// From the null forward (null, 0, 0, 0) are optional parameters
Mock.Arrange(() => DataAccess.LogEventToDatabase(0, 0,
null
, 0, 0, 0)) .IgnoreArguments()
.Returns(1)
.OccursAtLeast(1);
Mock.Assert(() => DataAccess.LogEventToDatabase(0, 0,
null
, 0, 0, 0), Args.Ignore(), Occurs.AtLeastOnce());
I tested your snippet, and it failed with "Occurrence expectation failed. Expected at least 1 call. Calls so far: 0", as expected. What do you mean by saying that it doesn't work?
Regards,
Stefan
Telerik

To assist you further, I would suggest checking this article from our online help documentation. There, you will be able see some good examples in the AAA (Arrange/Act/Assert) pattern.
Also, here is explained how to mock static members with JustMock.
I hope you find this helpful. Let me know if there is anything else I can assist you with.
Regards,
Kaloyan
Telerik
Is there not a way to do this for all static methods all at once? With instance members it is enough to assert the mock object like below...
Mock.Assert(mockObject)
However the above doesn't work with static methods and properties... the occurrence expectations on static members will throw an exception if that member is actually called (and the expectation fails), but if you setup mocks for static members that do not get called those occurrence expectations are never evaluated. This can lead to having several mocks setup that are unnecessary. Is the only way to prevent this to have a Mock.Assert() call for each static member at the end? I.E.
Mock.Arrange(() => StaticClass.StaticMethod1()).Returns(null).Occurs(1);
Mock.Arrange(() => StaticClass.StaticMethod2()).Returns(null).Occurs(2);
Mock.Arrange(() => StaticClass.StaticMethod3()).Returns(null).Occurs(3);
Mock.Arrange(() => StaticClass.StaticMethod4()).Returns(null).Occurs(4);
...
StaticClass.StaticMethod1();
StaticClass.StaticMethod2();
StaticClass.StaticMethod2();
StaticClass.StaticMethod2();
StaticClass.StaticMethod3();
StaticClass.StaticMethod3();
...
Mock.Assert(StaticClass.StaticMethod1(), Args.Ignore(), Occurs.Exactly(1));
Mock.Assert(StaticClass.StaticMethod2(), Args.Ignore(), Occurs.Exactly(2)); => Will fail because it was called 3 times
Mock.Assert(StaticClass.StaticMethod3(), Args.Ignore(), Occurs.Exactly(3)); => Will fail because it was called 2 times.
// StaticClass.StaticMethod4() was never called, so the occurrence expectation that it was called 4 times is never evaluated.
Mock.AssertStatic(StaticClass);
Mock.Assert(typeof(StaticClass);
Is it possible to disable the behavior where the mocking framework will automatically assert occurrence expectations set for static methods? It would be nice if all occurrence expectations would only be evaluated when I call Mock.Assert.
Hello James,
If I understand you correctly, you are asking for something explained below.
Let's have the following test:
[TestMethod]
public void TestMethod1()
{
Mock.Arrange(() => StaticClass.StaticMethod1()).DoNothing().Occurs(2);
StaticClass.StaticMethod1();
//Mock.Assert<StaticClass>();
}
The test will succeed unless you uncomment the line containing Mock.Assert. This is how the product currently works. If this is not what you are looking for, please clarify with a sample.
That is correct, however the below will evaluate automatically when StaticMethod1 is called.
[TestMethod]
public void TestMethod1()
{
Mock.Arrange(() => StaticClass.StaticMethod1()).DoNothing().Occurs(0);
StaticClass.StaticMethod1();
}
Hello James, after reviewing the source code more closely, it appears that the public API does not provide a way to disable automatic occurrence assertions. This is a great point for potential improvements in the future. As a workaround, you can set the lower bound for occurrences assertion to 0 and use a specific assertion to check for the exact number of occurrences if needed. It would look something like this:
[TestMethod]
public void TestMethod1()
{
Mock.Arrange(() => StaticClass.StaticMethod1()).DoNothing().OccursAtLeast(0);
StaticClass.StaticMethod1();
StaticClass.StaticMethod1();
// used for exact occurrence assertion, uncomment it if needed
//Mock.Assert(() => StaticClass.StaticMethod1(), Occurs.Exactly(2));
}