I'm getting an error message in Moq
Message: Test method [...] threw exception:
Moq.MockException: IVeracrossAPI.Authorization = Basic Og== invocation failed with mock behavior Strict.
All invocations on the mock must have a corresponding setup.
Seems obvious. Unfortunately, in my code I already have this:
var VeracrossMock = new Mock<IVeracrossAPI>(MockBehavior.Strict);
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
VeracrossMock.Setup(a => a.Authorization).Returns(new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)));
So now I'm not sure how IVeracrossAPI.Authorization
is missing. What's an approach to figure out what is really going on?
When using MockBehavior.Strict
it means that for a mock any member being invoked must have a corresponding Setup
or it will throw an exception.
When you use MockBehavior.Loose
, which is also the default, then when you call a member that was not setup then it will just return the default type of the member result.
The current setup shown will work for getting the value from the property.
From the exception it appears the test tries to set the value which has not been setup. Remember properties are basically two methods in one (get_Property and set_Property).
Suggest stubbing the property so that it keeps track of assigned values.
// start "tracking" sets/gets to this property
VeracrossMock.SetupProperty(_ => _.Authorization);
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
var authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
//set value
VeracrossMock.Object.Authorization = authorization;
Reference Moq Quickstart: Properties
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.