I can't figure out how to Assert the number of occurrences on a static Mock. The below doesn't work.
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
0
Hi Robert,
Thank you for contacting our support.
To answer your question, I'll use the following system under test for example:
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
If the above Args.Ignore() is not specified, the test will fail. This is so, because the assert will expect that the
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
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
0
Robert
Top achievements
Rank 1
answered on 09 Oct 2013, 06:03 PM
That doesn't seem to work for the following...
Mock.Arrange(() => DataAccess.UpdateUserStatus(
null
))
.IgnoreArguments().IgnoreInstance()
.Returns(1)
.OccursNever();
Mock.Assert(() => DataAccess.UpdateUserStatus(
null
), Args.Ignore());
0
Robert
Top achievements
Rank 1
answered on 09 Oct 2013, 06:11 PM
I guess because I should be doing this...
Mock.Assert(() => DataAccess.UpdateUserStatus(
null
), Args.Ignore(), Occurs.Never());
0
Robert
Top achievements
Rank 1
answered on 09 Oct 2013, 07:01 PM
However this doesn't work...
// 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());
0
Hello Robert,
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
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
0
Robert
Top achievements
Rank 1
answered on 10 Oct 2013, 01:09 PM
It is working. I was making a mistake. thanks.
0
Hello Robert,
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
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