Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive NS1004 with assignment check on virtual property #171

Closed
mariuszlip opened this issue Oct 1, 2021 · 2 comments
Closed

False positive NS1004 with assignment check on virtual property #171

mariuszlip opened this issue Oct 1, 2021 · 2 comments
Milestone

Comments

@mariuszlip
Copy link

Hello, I received a warning for a check of "not assigned" property, it's related to #159 , but with Arg.Any<>() method
I managed to reproduce it in simpler project:

using System.Collections.Specialized;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using NSubstitute;
using NUnit.Framework;

namespace Example
{
    public class MyController : Controller
    {
        public void Save()
        {
        }
    }

    [TestFixture]
    public class UnitTest
    {
        private MyController TestedInstance { get; set; }

        [SetUp]
        public virtual void SetUp()
        {
            TestedInstance = new MyController();
        }

        [Test]
        public void ReturnEmptyOkResponseAfterSaving()
        {
            HttpResponseBase response = null;

            var controllerExecutionData = ConfigureTestedInstanceRequest((httpContext, requestContext) =>
            {
                response = httpContext.Response;

                requestContext.RouteData.Values["action"] = nameof(TestedInstance.Save);
            });

            (TestedInstance as IController).Execute(controllerExecutionData);

            response.DidNotReceive().StatusCode = Arg.Any<int>();
        }
        
        private RequestContext ConfigureTestedInstanceRequest(Action<HttpContextBase, RequestContext> configure)
        {
            var httpContextMock = Substitute.For<HttpContextBase>();
            httpContextMock.Request.Form.Returns(callInfo => Substitute.For<NameValueCollection>());
            httpContextMock.Request.QueryString.Returns(callInfo => Substitute.For<NameValueCollection>());
            httpContextMock.Request.Headers.Returns(callInfo => Substitute.For<NameValueCollection>());
            
            var routeDataMock = Substitute.For<RouteData>();

            var requestContextMock = Substitute.For<RequestContext>();
            requestContextMock.HttpContext.Returns(callInfo => httpContextMock);
            requestContextMock.RouteData.Returns(callInfo => routeDataMock);

            configure(httpContextMock, requestContextMock);

            return requestContextMock;
        }
    }
}

The warning I get is:
%userprofile%\Documents\Example\Example\UnitTest.cs(44,51,44,65): warning NS1004: Argument matcher used with a non-virtual member of a class.

@tpodolak
Copy link
Member

Hi @mariuszlip
Thanks for reporting. As you mentioned, it is indeed related to #159. Fix is already in review, I hope to release new version this month

@tpodolak
Copy link
Member

Fixed in #169

@tpodolak tpodolak added this to the 1.0.15 milestone Nov 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants