moles

Using Moles with DateTime

痞子三分冷 提交于 2019-11-30 22:01:08
I'm starting to using Moles in unit tests and am struggling a little with documentation. I want to mole DateTime.Now. If you look around the old way of doing this was to add a reference to mscorlib, then add a stubx file for it (Add New Item -> Stubs And Moles For Testing). The 'Stubs and Moles for Testing' template has been deprecated, instead all you need do is to right click a reference and select 'Add moles assembly', whch is fine. VS2010 does not allow you to add a reference directly to mscorlib, because we have a reference to "System", this is ok as I can see DateTime in object browser

mocking session variable in unit test using Moles

痴心易碎 提交于 2019-11-29 11:56:09
Method I am unit testing checks for a session variable like if(Session["somevar"] != null) { // rest of the code } In my test, not able to get rid of this since Session is null, it's throwing null referrence exception. To bypass this, I have tried mocking it like below but no luck System.Web.Moles.MHttpContext.AllInstances.SessionGet = (HttpContext cntx) => { return (HttpSessionState)cntx.Session["somevar"]; } I even tried method mention here to simulate HttpContext and then doing below HttpContext.Current = new HttpContext(workerRequest); HttpContext.Current.Session["somevar"] = value; But

No Code Coverage Information for Tests Using Moles

家住魔仙堡 提交于 2019-11-28 14:48:02
I have been getting used to OpenCover over the past few days, and have I noticed that tests using Moles do not generate any Coverage information. I have created a small solution to isolate the problem, and have found that code coverage is generated only for tests that do not have the [HostType("Moles")] attribute. Reading around I have found this and this which seem to be NCover equivalents of my problem. They say that it is something to do with Moles running a profiler as well as the coverage tool, and that there is an environment variable CLRMONITOR_EXTERNAL_PROFILERS that can be set to

mocking session variable in unit test using Moles

不想你离开。 提交于 2019-11-28 05:33:35
问题 Method I am unit testing checks for a session variable like if(Session["somevar"] != null) { // rest of the code } In my test, not able to get rid of this since Session is null, it's throwing null referrence exception. To bypass this, I have tried mocking it like below but no luck System.Web.Moles.MHttpContext.AllInstances.SessionGet = (HttpContext cntx) => { return (HttpSessionState)cntx.Session["somevar"]; } I even tried method mention here to simulate HttpContext and then doing below

How Moles Isolation framework is implemented?

大憨熊 提交于 2019-11-27 11:49:49
Moles is an isolation framework created by Microsoft. A cool feature of Moles is that it can "mock" static/non-virtual methods and sealed classes (which is not possible with frameworks like Moq). Below is the quick demonstration of what Moles can do: Assert.AreNotEqual(new DateTime(2012, 1, 1), DateTime.Now); // MDateTime is part of Moles; the below will "override" DateTime.Now's behavior MDateTime.NowGet = () => new DateTime(2012, 1, 1); Assert.AreEqual(new DateTime(2012, 1, 1), DateTime.Now); Seems like Moles is able to modify the CIL body of things like DateTime.Now at runtime. Since Moles

No Code Coverage Information for Tests Using Moles

随声附和 提交于 2019-11-27 08:56:27
问题 I have been getting used to OpenCover over the past few days, and have I noticed that tests using Moles do not generate any Coverage information. I have created a small solution to isolate the problem, and have found that code coverage is generated only for tests that do not have the [HostType("Moles")] attribute. Reading around I have found this and this which seem to be NCover equivalents of my problem. They say that it is something to do with Moles running a profiler as well as the

How Moles Isolation framework is implemented?

我的未来我决定 提交于 2019-11-26 15:46:58
问题 Moles is an isolation framework created by Microsoft. A cool feature of Moles is that it can "mock" static/non-virtual methods and sealed classes (which is not possible with frameworks like Moq). Below is the quick demonstration of what Moles can do: Assert.AreNotEqual(new DateTime(2012, 1, 1), DateTime.Now); // MDateTime is part of Moles; the below will "override" DateTime.Now's behavior MDateTime.NowGet = () => new DateTime(2012, 1, 1); Assert.AreEqual(new DateTime(2012, 1, 1), DateTime.Now