How totestinternalprotectmethodsinc#
Click here to load reader
-
Upload
learningtech -
Category
Technology
-
view
163 -
download
0
Transcript of How totestinternalprotectmethodsinc#
![Page 1: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/1.jpg)
How to test internal / protected methods in c#
Howard 2014/02/13
![Page 2: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/2.jpg)
Modifiers
• internal
• The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly
• protected
• The protected keyword is a member access modifier. A protected member is accessible within its class and by derived class instances.
![Page 3: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/3.jpg)
How?
![Page 4: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/4.jpg)
How to test internal methods?
![Page 5: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/5.jpg)
How to test internal methods?
• InternalsVisibleToAttribute
• Specifies that types that are ordinarily visible only within the current assembly are visible to a specified assembly.
• Namespace: System.Runtime.CompilerServices
![Page 6: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/6.jpg)
How to test internal methods?
![Page 7: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/7.jpg)
How to test internal methods?
![Page 8: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/8.jpg)
How to test protected methods?
![Page 9: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/9.jpg)
How to test protected methods?• 繼承 TestClass 後• 覆寫要測試的方法並將其 modifier 設為 public
• 測試 public method
![Page 10: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/10.jpg)
How to test protected methods?
![Page 11: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/11.jpg)
new Modifier
• When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class.
• When you hide an inherited member, the derived version of the member replaces the base class version.
• Although you can hide members without using the new modifier, you get a compiler warning. If you use new to explicitly hide a member, it suppresses this warning.
![Page 12: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/12.jpg)
How to test protected methods? – better solution!
Set method modifier to protected internal, and use InternalsVisibleToAttribute
• protected internal: Access is limited to the current assembly or types derived from the containing class.
![Page 13: How totestinternalprotectmethodsinc#](https://reader038.fdocuments.in/reader038/viewer/2022100600/5555d2e3d8b42a711f8b4b09/html5/thumbnails/13.jpg)
References• How to Test Private and Protected methods in .NET
• http://www.codeproject.com/Articles/9715/How-to-Test-Private-and-Protected-methods-in-NET
• InternalsVisibleToAttribute Class
• http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx
• Modifiers
• http://msdn.microsoft.com/en-us/library/6tcf2h8w.aspx
• new Modifier
• http://msdn.microsoft.com/en-us/library/435f1dw2.aspx