C # unit test code questions go on

more questions after questions here: C # unit test code questions

I found VS unit test testframe method private

and protected

in the same way, but rejected from the method public

.

Below is the generated code for the method private

:

        /// <summary>
        ///A test for recordLogin
        ///</summary>
        [TestMethod()]
        [DeploymentItem("SystemSoftware.exe")]
        public void recordLoginTest()
        {
            User_Accessor target = new User_Accessor(); // TODO: Initialize to an appropriate value
            Guid userId = new Guid(); // TODO: Initialize to an appropriate value
            string action = string.Empty; // TODO: Initialize to an appropriate value
            Users user = null; // TODO: Initialize to an appropriate value
            AndeDBEntities db = null; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.recordLogin(userId, action, user, db);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

      

questions:

  • [DeploymentItem("SystemSoftware.exe")]

    for methods private

    and protected

    why is it needed and what is it for?

  • In my original class / file, if I point to the original method and try " Find All References

    ". The link in the unit test class / file will not show for methods private

    and protected

    , but it will show for all methods public

    . Why is this? Is it correct?

+2


a source to share


3 answers


[DeploymentItem ("SystemSoftware.exe")] for private and protected methods, why and what is it for?

You cannot access private, protected, or internal members from a unit test that is in a different assembly and does not inherit from the class you are trying to test (and this will not be possible if your testing "block" is more than one class) ... To access private, protected, or internal members, MSTest will create an accessory assembly that will give you proxy access to those hidden members.

... DeploymentItemAttribute signals the test runner that artifacts (and dependencies such as accessory collectors or test data files) must be deployed for the code to execute correctly. Essentially, it implicitly tells MSTest to build and deploy the access assembly in this case.



In my original class / file if I point to the original method and try to "Find All References". The link in the unit test class / file will not appear for private and protected methods, but it will appear for all public methods. Why is this? Isn't that right?

See above, you don't have direct access to them, but use a proxy to do so. This proxy uses runtime reflection to bind your call, so this cannot be traced back inside Visual Studio.

+2


a source


[DeploymentItem ("SystemSoftware.exe")] is for private and secure methods, why is it and what is it for?

It defines the (file) resources needed for testing (you can apply to the test class or individual methods). Since testing methods are supposed to be publicly available, I don't understand why you would apply this to a method private

or protected

.



The attribute is documented: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute.aspx

+1


a source


Find All References

won't find these tests because they use the accessor ( User_Accessor

) class instead of the real class ( User

) to access the protected

and methods private

. The accessor class is automatically generated and does some tricks to expose those methods that would normally not be accessible.

+1


a source







All Articles