{"id":377,"date":"2009-05-07T20:38:08","date_gmt":"2009-05-07T19:38:08","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=377"},"modified":"2021-03-11T09:44:41","modified_gmt":"2021-03-11T08:44:41","slug":"mocks-for-internal-interfaces","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2009\/05\/mocks-for-internal-interfaces\/","title":{"rendered":"Mocks for internal interfaces"},"content":{"rendered":"<p>Maybe you\u2019ve already seen an exception like this:<\/p>\n<blockquote><p>Castle.DynamicProxy.Generators.GeneratorException: Type is not public, so a proxy cannot be generated. Type: SpielWiese.IReportSender<br \/>\n\u2026stack-trace\u2026<br \/>\nat SpielWiese.TestSomething.ExpectReportsSend() in Program.cs: line 64<\/p><\/blockquote>\n<p>What I\u2019ve tried is to create a mock-object with <a href=\"http:\/\/ayende.com\/projects\/rhino-mocks.aspx\">Rhino-Mock<\/a>. Since this interface is only intended for usage within the assembly it\u2019s declared as internal interface. So Rhino-Mock fails and I really don\u2019t want to create a mock manually. The quickest and dirtiest way would be to make the interface public. But I really don\u2019t want to leak internal stuff just for tests.<\/p>\n<p>So the second approach is to make the internals visible to Rhino-Mocks.<\/p>\n<blockquote>\n<pre class=\"csharpcode\">[assembly: InternalsVisibleTo(<span class=\"str\">\"Rhino.Mocks\"<\/span>)]<\/pre>\n<\/blockquote>\n<p>But it will fail as well. Since Rhino uses Castle-Proxies, I tried:<\/p>\n<blockquote>\n<pre class=\"csharpcode\">[assembly: InternalsVisibleTo(<span class=\"str\">\"Castle.DynamicProxy\"<\/span>)]<\/pre>\n<\/blockquote>\n<p>It still won\u2019t work. The reason for this is simple. The Mock-Factory has to generate code. The generated code is hosted in a separate assembly. So you have to make the dynamic assembly visible to your code under test.<\/p>\n<blockquote>\n<pre class=\"csharpcode\">[assembly: InternalsVisibleTo(<span class=\"str\">\"DynamicProxyGenAssembly2\"<\/span>)]<\/pre>\n<\/blockquote>\n<p>This is similar with other mock frameworks or proxy-frameworks.<\/p>\n<p>Source: <a title=\"http:\/\/ayende.com\/Wiki\/Rhino+Mocks+-+Internal+Methods.ashx\" href=\"http:\/\/ayende.com\/Wiki\/Rhino+Mocks+-+Internal+Methods.ashx\">http:\/\/ayende.com\/Wiki\/Rhino+Mocks+-+Internal+Methods.ashx<\/a><\/p>\n<p>Demo-Code: <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/program.cs\">demo-code<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Maybe you\u2019ve already seen an exception like this: Castle.DynamicProxy.Generators.GeneratorException: Type is not public, so a proxy cannot be generated. Type: SpielWiese.IReportSender \u2026stack-trace\u2026 at SpielWiese.TestSomething.ExpectReportsSend() in Program.cs: line 64 What I\u2019ve&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[126,17],"tags":[21,24,107,23],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/377"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":4,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"predecessor-version":[{"id":381,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/377\/revisions\/381"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}