简体   繁体   中英

Pass nested class by reference as parameter in unit test method

So I'm trying to dive into Unit Testing, specifically for web services (SOAP versions), and am having incredible difficulty in getting the appropriate format for passing the referenced nested class OtherClass.aNestedClass.

As you can see below, both of the classes OtherClass and aNestedClass are public. The method readDB is the target method for the unit test. However, when I attempt to declare an instance of OtherClass, I only get the option of OtherClass_Accessor, which makes the readDB method call none too happy (specifically I get the 'The best overloaded match for ... has some invalid arguments' error).

How do I go about correctly passing a class by reference as a parameter to a unit test's accessor method call?

public class MyClass
{
    public void readDB(ref OtherClass.aNestedClass oa)
    {
        oa.A = "abc";
        oa.B = "def";
    }
}

public class OtherClass
{
    public class aNestedClass
    {
        public string A;
        public string B;
    }
}

My sample attempt:

[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("D:\\MyProject", "/MyProject")]
[UrlToTest("http://localhost/MyProject/default.aspx")]
public void readDBTest()
{
    OtherClass_Accessor.aNestedClass oa = new OtherClass_Accessor.aNestedClass();

    readDB(ref oa);

    // assert statements...
}

The parameter in readDB() is specifying a type , not a parameter variable. Try this:

public class MyClass
{
    public void readDB(ref OtherClass.aNestedClass myNestedClassInstance)
    {
        //do some DB reading, modify strings in class passed by ref.
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM