{"id":7,"date":"2015-07-26T09:23:00","date_gmt":"2015-07-26T09:23:00","guid":{"rendered":""},"modified":"2016-01-30T22:38:18","modified_gmt":"2016-01-30T22:38:18","slug":"c-testing-internal-classes","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2015\/07\/26\/c-testing-internal-classes\/","title":{"rendered":"C# &#8211; testing internal classes"},"content":{"rendered":"<p>I am currently involved in writing library which will be used by couple of external teams. In that library we have a quite a lot classes which are only used internally and won&#8217;t be a part of public <i>API<\/i>. The natural way of keeping that code inaccessible for other assemblies is to use internal access modifier. However this makes the code also inaccessible directly for testing. Of course you can still test your internal code through public <i>API<\/i> but usually this is inconvenient and forces you to write a lot of additional code in test. This is particularly painful for me, because our build process requires 100% test coverage. Fortunately there is an easy way to make internal classes visible for certain assemblies without making them public. All you have to do is to use <i>InternalsVisibleToAttribute<\/i> and specify assembly name which is allowed to use code with internal access modifier. This attribute is most commonly added to <i>AssemblyInfo.cs<\/i> file. For example, assuming that my library is in <i>TestingInternalClasses.Lib<\/i> assembly and my tests are in <i>TestingInternalClasses.Tests<\/i> I should add<\/p>\n<pre>\r\n[assembly: InternalsVisibleTo(\"TestingInternalClasses.Tests\")]\r\n<\/pre>\n<p>to <i>AssemblyInfo.cs<\/i> located in <i>TestingInternalClasses.Lib<\/i> project. However you can also use this attribute in a class definition file &#8211; just like that<\/p>\n<pre>\r\nusing System;\r\nusing System.IO;\r\nusing System.Runtime.CompilerServices;\r\n[assembly: InternalsVisibleTo(\"TestingInternalClasses.Tests\")]\r\n\r\nnamespace TestingInternalClasses.Lib\r\n{\r\n    internal class VanillaSerializer : ISerializer\r\n    {\r\n        public Stream Serialize(object obj)\r\n        {\r\n            if (obj == null)\r\n                throw new ArgumentNullException(nameof(obj));\r\n\r\n            \/\/ rest of the code\r\n            \/\/ ...\r\n            \/\/ ...\r\n        }\r\n\r\n        public T Deserialize(Stream stream)\r\n        {\r\n            if (stream == null)\r\n                throw new ArgumentNullException(nameof(stream));\r\n\r\n            \/\/ rest of the code\r\n            \/\/ ...\r\n            \/\/ ...\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>From now on I can easily test internal classes from <i>TestingInternalClasses.Lib<\/i> project in <i>TestingInternalClasses.Tests<\/i>. Source code for this post can be found <a href=\"https:\/\/github.com\/tpodolak\/Blog\/tree\/master\/TestingInternalClasses\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am currently involved in writing library which will be used by couple of external teams. In that library we have a quite a lot classes which are only used internally and won&#8217;t be a part of public API. The natural way of keeping that code inaccessible for other assemblies is to use internal access [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,11],"tags":[160,159,166],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-unittest","tag-net","tag-c","tag-unittest"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":7,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}