{"id":1964,"date":"2022-08-30T15:20:55","date_gmt":"2022-08-30T15:20:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/05\/problem-about-moq-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:55","modified_gmt":"2022-08-30T15:20:55","slug":"problem-about-moq-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/problem-about-moq-collection-of-common-programming-errors\/","title":{"rendered":"problem about moq-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/31f9c461f411969e33ca6bd13b536360?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJohn MacIntyre<br \/>\nc# unit-testing testing mocking moq<br \/>\nSo I copied the sample code from the Moq home page pretty much verbatim, and am getting a castle proxy exception.Here&#8217;s my code (as a console app for an easier sample)using System; using System.Collections.Generic; using System.Linq; using System.Text; using Moq;namespace MoqTestConsole {public interface ILoveThisFramework{bool DownloadExists(string s);}class Program{static void Main(string[] args){Mock&lt;ILoveThisFramework&gt; mock = new Mock&lt;ILoveThisFramework&gt;();\/\/ WOW! No record\/repla<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4e51620640a9e6b4fae81b1189a41e37?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRafael Steil<br \/>\nc# generics mono moq castle-dynamicproxy<br \/>\nI found a really strange problem while creating unit tests that only occurs with the Mono runtime (Xamarin on Mac included), but runs fine within Visual Studio. I isolated it as far as I could, and I reached a point that I can&#8217;t tell if it is a bug with Mono, Moq or Castle DinamicProxy, although it only crashes when using the Mono runtime. This is the code:using System; using System.Collections.Generic; using Moq;namespace ConsoleApplication1 {public interface ISomething&lt;T&gt;{List&lt;T&gt; D<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5bda6fbfe012c78d7b16fce7d9a4d377?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nchiccodoro<br \/>\nasp.net unit-testing error-handling moq<br \/>\nShort Version:If I create a System.Web.HttpException as follows:var exception = new HttpException(403, &#8220;Forbidden&#8221;);I would expect the following methods to return these values, but they don&#8217;t:var code = exception.GetHttpCode(); \/\/ is 0 var msg = exception.GetHtmlErrorMessage(); \/\/ is: nullEdit: In fact GetHttpCode() returns the correct number when being called the first time, but returns 0 when being called a second time:var code = exception.GetHttpCode(); \/\/ is 403 code = exception.GetHttpCode(<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/eaeda823091956681c74daed0e0d12e5?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nBryanGrimes<br \/>\nc# unit-testing moq<br \/>\nI have, what I think, is a pretty straight forward setup in which a search type is created and passed through a service layer and into a repository where a list of a domain type is returned. The search type does nothing but construct an expression tree in the repository method and basically the results from the database come back. Pretty simpleThe repository interface:public interface IDoNotSolicitRepo {IList&lt;DNSContract&gt; SelectWithCriteria(DNS_Search searchriteria); }The service imple<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/3883a62c7ddc319ffe266e14ef7ecd5b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ncedd<br \/>\nc# reflection moq<br \/>\nIs it possible to write code like the following. I&#8217;m trying to using Moq with objects that I&#8217;m reflecting on as part of a testing framework. The code below raises a &#8220;Unhandled expression type: &#8216;Goto'&#8221; exception from Moq, which I guess is expecting something different. It kind of looks like it should work though!private void button1_Click(object sender, EventArgs e){Ifoo = foo Foo();\/\/ Create input parameter for lambdaParameterExpression value = Expression.Parameter(typeof(IFoo), &#8220;value&#8221;);\/\/ cre<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b9da2ca6abf3a1e28fb470f2b3ae2c34?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nabatishchev<br \/>\nc# .net unit-testing setup moq<br \/>\nI have these mocks:_processWrapperMock = new Mock&lt;IProcessWrapper&gt;(MockBehavior.Strict); _processStartInfoMock = new Mock&lt;IProcessStartInfo&gt;();and a setup for the Start() method:_processWrapperMock.Setup(m =&gt; m.Start(_processStartInfoMock.Object)).Returns(new Process());and my _processWrapperMock instance is passed to the constructor for the object which contains the method I&#8217;m testing.var wrapper = new WrapperClassImTesting(_processWrapperMock.Object);I then execute the method wh<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/d550dbe9c5f96baae91a41323914341e?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nmosquito87<br \/>\nasp.net-mvc asp.net-mvc-3 unit-testing moq mvcmailer<br \/>\nI&#8217;d like to unit-test a method which is calling a method from the MvcMailer.I get the following error message:ArgumentNullException was unhandled by user code. Value cannot be null. Parameter name: routeDataThis is how I mock the Mailer with Moq:protected void InitializeMailer() {userMailer = new Mock&lt;UserMailer&gt;();userMailer.CallBase = true; }And my test method where (in the last line) the method is called which calls the mailer method:public void CreateUseraccountTest() {\/\/Setup mailerus<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/16961c15df3fee8c4b3056ff83d1938b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nNullUserException<br \/>\nasp.net-mvc unit-testing asp.net-mvc-2 moq<br \/>\nI&#8217;m using Moq to help in testing my ASP.NET MVC2 application.Problem: ArgumentException was unhandled by user code. Unable to obtain public key for StrongNameKeyPairThis code has been adapted from Scott Hanselman&#8217;s NerdDinner1.HomeController CreateHomeControllerAs(string userName){var mock = new Mock&lt;ControllerContext&gt;();mock.SetupGet(p =&gt; p.HttpContext.User.Identity.Name).Returns(userName); \/\/ fails heremock.SetupGet(p =&gt; p.HttpContext.Request.IsAuthenticated).Returns(true);var c<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/228650c89928a3fb6d269ac3de839cb8?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\niCe<br \/>\nc# tdd moq<br \/>\nI\u00b4m trying to do TDD with an object that has a dependency on a COM Interface. I though about mocking the COM interface, while doing development testing, and do it real on the integration tests. However, I cannot mock the COM interface, I tried with Moq, and it throws an exception:System.TypeLoadException was unhandledby user code Message=Could not load type &#8216;Castle.Proxies.iTunesAppProxy&#8217;from assembly &#8216;DynamicProxyGenAssembly2,Version=0.0.0.0, Culture=neutral,PublicKeyToken=null&#8217;. The type ismar<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8d2a69c7391254ec05da64052c94f3d9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nErik Dietrich<br \/>\nc# asp.net unit-testing moq<br \/>\nI am using hanselman tutorial to use Moq to create unit tests for my asp.net pages. I wrote the following code to test for ServerVariables in contextbase request class HttpContextBase contextbase = MoqHelper.FakeHttpContext(); contextbase.Request.ServerVariables.Add(&#8220;AUTH_TYPE&#8221;,&#8221;Forms&#8221;); &lt;&#8211; error here contextbase.Request.ServerVariables.Add(&#8220;LOGON_USER&#8221;, &#8220;Tom&#8221;); contextbase.Request.ServerVariables.Add(&#8220;REQUEST_METHOD&#8221;, &#8220;GET&#8221;);But I am getting following exception. Please help.System.NullRefer<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/c4nQF.jpg?s=32&amp;g=1\" \/><br \/>\nTom Stickel<br \/>\nmoq mstest<br \/>\nI am using MSTest to write unit tests, but I have been tasked with adding moq to the unit test. I understand that if an integration test is done in which a file system, a database is normally called, that mocking is essentially allowing for the testing without actually making those &#8220;real&#8221; calls. I have looked around and just need some help.I was starting with some basic utilities that I found and started with some basic testing of them with Asserts. However, I&#8217;m needing to take it to the next<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5a461eb4e1765d5968ce2a978e1c9f81?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJames Hay<br \/>\nc# unit-testing mocking moq<br \/>\nI have a switch statement in a factory that returns a command based on the value of the enum passed in. Something like:public ICommand Create(EnumType enumType) {switch (enumType){case(enumType.Val1):return new SomeCommand();case(enumType.Val2):return new SomeCommand();case(enumType.Val3):return new SomeCommand();default:throw new ArgumentOutOfRangeException(&#8220;Unknown enumType&#8221; + enumType);} }I currently have a switch case for each value in the enum. I have unit tests for each of these cases. How<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2013-12-05 16:32:52. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>John MacIntyre c# unit-testing testing mocking moq So I copied the sample code from the Moq home page pretty much verbatim, and am getting a castle proxy exception.Here&#8217;s my code (as a console app for an easier sample)using System; using System.Collections.Generic; using System.Linq; using System.Text; using Moq;namespace MoqTestConsole {public interface ILoveThisFramework{bool DownloadExists(string s);}class Program{static void [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1964","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1964"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1964\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}