Tag: unit testing

尝试获取数据库类型的实例时出现激活错误,键“”< – 空白

我正在尝试使用Enterprise Library 5.0并在我的BL上进行一些unit testing,我是否需要在DL或Test项目上安装app.config? 注意:我的web项目上的web.config上已经有配置设置。 我如何使用DAAB: private static Database db = DatabaseFactory.CreateDatabase(); db.ExecuteNonQuery(“spInsertSalesman”, salesman.Fullname); 我在DL上的app.config:

如何在ASP.NET MVC中进行测试时访问JsonResult数据

我在C#mvc控制器中有这个代码: [HttpPost] public ActionResult Delete(string runId) { if (runId == “” || runId == null) { return this.Json(new { error = “Null or empty params” }); } try { int userId = (int)Session[“UserId”]; int run = Convert.ToInt32(runId); CloudMgr cloud = new CloudMgr(Session); cloud.DeleteRun(userId, run); return this.Json(new { success = true }); } catch (Exception ex) […]

Visual Studio 2012伪造的UnitTestIsolation工具无法初始化

刚刚安装vs 2012更新2( http://www.microsoft.com/en-us/download/details.aspx?id=36833 ),所以我可以使用vs fakes / shims来测试一些难以测试的代码。 当我创建假assembly并且所有引用都按预期添加到unit testing项目中时,一切都编译得很好。 但是在任何测试中运行以下代码…… using (ShimsContext.Create()) { //Doesn’t matter whats in here } 引发以下exception…… Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException : UnitTestIsolation instrumentation failed to initialize. Please restart Visual Studio and rerun this test 堆栈跟踪完全exception…… Test ‘Abot.Tests.Unit.Core.HapHyperLinkParserTest.HyperLinkParserTest.GetLinks_AreaTags_ReturnsLinks’ failed: Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException : UnitTestIsolation instrumentation failed to initialize. Please restart Visual Studio and rerun this test at […]

无法在Visual Studio 2012中调试unit testing项目

我找不到类似的post,所以我希望这不是重复的。 我有一个ac#类库,我正在尝试在Visual Studio 2012中运行unit testing。我在我的解决方案中添加了一个新的unit testing项目,并在那里添加了我的主项目作为参考。 我已将我的unit testing项目设置为启动项目。 当我尝试调试时,收到错误消息 无法直接启动具有类库的输出类型的项目。 要调试此项目,请向此解决方案添加可执行项目,该解决方案引用库项目。 将可执行项目设置为启动项目。 根据msdn的演练,我应该在调试时运行测试。 有什么想法吗? 这是我的unit testing代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; using Common; using Messages; namespace MessageUnitTests { [TestClass] class RegistrationTester { [TestMethod] public void RegistrationRequest_TestConstructorsAndFactories() { RegistrationRequest rr1 = new RegistrationRequest(“myhandle”); Assert.AreEqual(“myhandle”, rr1.Handle); rr1 = new RegistrationRequest(“longHandle-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789’|;:’,.=-_+!@#$%^&*()”); Assert.AreEqual(“longHandle-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789’|;:’,.=-_+!@#$%^&*()”, rr1.Handle); […]

Rhino Mocks – 如果调用方法,则设置属性

如果调用方法,有没有办法让Rhino Mocks设置Stub的属性。 这样的事情:(粗体假代码) callMonitor.Expect(X => x.HangUp())。 SetProperty(callMonitor.InACall = false) ; HangUp方法返回void,我无法真正改变它。 但是我希望我的存根知道在调用HangUp时挂断了呼叫。

ASP.NET MVCunit testing自定义AuthorizeAttribute

我正在研究ASP.NET MVC 4项目(.NET框架4),我想知道如何正确地unit testing自定义AuthorizeAttribute(我使用NUnit和Moq)。 我AuthorizeCore(HttpContextBase httpContext)了2个方法: AuthorizeCore(HttpContextBase httpContext)和HandleUnauthorizedRequest(AuthorizationContext filterContext) 。 正如您所看到的,这些方法分别需要HttpContextBase和AuthorizationContext ,但我不知道如何模拟它们。 这是我得到的: [Test] public void HandleUnauthorizedRequest_UnexistingMaster_RedirectsToCommonNoMaster() { // Arrange var httpContext = new Mock(); var winIdentity = new Mock(); winIdentity.Setup(i => i.IsAuthenticated).Returns(() => true); winIdentity.Setup(i => i.Name).Returns(() => “WHEEEE”); httpContext.SetupGet(c => c.User).Returns(() => new ImdPrincipal(winIdentity.Object)); // This is my implementation of IIdentity var requestBase […]

使用Moq在C#中设置索引器

我无法弄清楚如何使用Moq在C#中设置索引器 。 Moq文档很薄弱,我已经做了很多搜索……我想做的事情在如何设置索引属性的解决方案中类似: var someClass = new Mock(); someClass.SetupSet(o => o.SomeIndexedProperty[3] = 25); 我想修改上面的内容以适用于任何索引和任何值,所以我可以这样做: someClass.Object.SomeIndexedProperty[1] = 5; 目前我有以下内容,它对索引属性getter非常有用,但如果我设置了值,则Moq会忽略它: var someValues = new int[] { 10, 20, 30, 40 }; var someClass = new Mock(); someClass.Setup(o => o.SomeIndexedProperty[It.IsAny()]) .Returns(index => someValues[index]); // Moq doesn’t set the value below, so the Assert fails! someClass.Object.SomeIndexedProperty[3] = 25; Assert.AreEqual(25, […]

将unit testing写入assembly或单独的assembly中?

在编写unit testing时,您是否将测试放在要测试的组件内或单独的测试组件中? 我已经在一个单独的程序集中编写了一个带有测试的应用程序,以便于部署,因为我可以排除程序集。 有没有人在你想测试的程序集中写那些测试,如果有的话,它的理由是什么?

使用Moq覆盖同一类中的虚方法

我们使用Moq对我们的服务类进行unit testing,但是他们仍然坚持如何测试服务方法调用同一类的另一个服务方法的情况。 我尝试将被调用的方法设置为虚拟,但仍然无法弄清楚在Moq中要做什么。 例如: public class RenewalService : IRenewalService { //we’ve already tested this public virtual DateTime? GetNextRenewalDate(Guid clientId) { DateTime? nextRenewalDate = null; //… a ton of already tested stuff… return nextRenewalDate; } //but want to test this without needing to mock all //the methods called in the GetNextRenewalDate method public bool IsLastRenewalOfYear(Renewal renewal) […]

在Visual Studio的快递版本中进行TDD的最佳方式(例如VB Express)

我一直在寻找为我正在编写的一个应用程序(OLE对象的OLE包装器)做一些测试驱动开发。 唯一的问题是我正在使用Visual Studio的快速版本(目前),目前我正在使用VB express,但有时我使用C#express。 是否可以在快递版本中进行TDD? 如果是这样的话,那是什么呢? 干杯。 编辑。 根据事物的外观我将不得不购买完整的视觉工作室,以便我可以做集成TDD,希望预算中有钱购买副本:)。 现在我想我会像所有人都说的那样使用Nunit。