سؤال

I'm trying to mock function like below:

AntiForgeryValidator a = new AntiForgeryValidator();
public void ValidateRequestHeader(HttpRequestBase request) 
{
    string cookieToken = "";
    string formToken = "";
    if (request.Headers["RequestVerificationToken"] != null)
    {
        string[] tokens = request.Headers["RequestVerificationToken"].Split(':');
        if (tokens.Length == 2)
        {
            cookieToken = tokens[0].Trim();
            formToken = tokens[1].Trim();
        }
    }
    a.Validate(cookieToken, formToken);
    //AntiForgery.Validate(cookieToken, formToken);
}

So I created interface:

public interface IAntiForgeryValidator
{
    //void ValidateRequestHeader(HttpRequestBase request);
    void Validate(string cookieToken, string formToken);
}
public class AntiForgeryValidator : IAntiForgeryValidator
{
    public void Validate(string cookieToken, string formToken)
    {
        AntiForgery.Validate(cookieToken, formToken);
    }
}

And add new code to the test project: Mocking Http Request:

Mock<HttpRequestBase> Request = new Mock<HttpRequestBase>();
Request.SetupGet(x => x.Headers).Returns(new System.Net.WebHeaderCollection{
    {"RequestVerificationToken", "RequestVerificationToken"}
});

And mock validate method:

Mock<IAntiForgeryValidator> antiForgeryMock = new Mock<IAntiForgeryValidator>();
antiForgeryMock.Setup(m => m.Validate(
    It.IsAny<string>(),
    It.IsAny<string>()))
    .Callback((string cookieToken, string formToken) =>
        {
            // call back
        });

antiForgeryMock.Verify(m => m.Validate(
        It.IsAny<string>(),
        It.IsAny<string>()), Times.Once());

This is all code I managed to create using answer found on this page. But I'm still getting error like in the topic. I suppose that all its caused by this lines:

.Callback((string cookieToken, string formToken) =>
        {
            // call back
        });

But I have no idea what should I put there. Reading Moq docs didn't help either.

@update: Trying to done this like creating fakeRepository

private IAntiForgeryValidator fakeValidation;
fakeValidation = antiForgeryMock.Object;

Function when I'm trying to fire tests:

[TestMethod]
public void EditPost()
{
    var data = default(Device);
    var result = DC.Edit(data);
    Assert.IsNotNull(result);
}

@Update two: added new code:

DC.ControllerContext = new ControllerContext(context.Object,new RouteData(),DC);

Now If I comment out Veryfy code works and is passed to controller. But still not working uncommented

هل كانت مفيدة؟

المحلول

You have to put the antiForgeryMock.Verify() after you've run your test action. A test usually looks like this:

// arrange
var myMock = new Mock<MyInterface>();
myMock.Setup(...);
var mySut = new Sut(myMock.Object);

// act
mySut.DoSomething()

//assert
myMock.Verify(...);

As mentioned in the comments, you don't need a Setup for your test, so you will skip that part.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top