Tag: nunit

在异步方法中测试exception

我对此代码有点困惑(这是一个示例): public async Task Fail() { await Task.Run(() => { throw new Exception(); }); } [Test] public async Task TestFail() { Action a = async () => { await Fail(); }; a.ShouldThrow(); } 代码没有捕获exception,并且失败了 期望抛出System.Exception,但没有抛出exception。 我确定我错过了一些东西,但是文档似乎暗示这是要走的路。 一些帮助将不胜感激。

如何模拟DbContext

这是我想要测试的代码 public DocumentDto SaveDocument(DocumentDto documentDto) { Document document = null; using (_documentRepository.DbContext.BeginTransaction()) { try { if (documentDto.IsDirty) { if (documentDto.Id == 0) { document = CreateNewDocument(documentDto); } else if (documentDto.Id > 0) { document = ChangeExistingDocument(documentDto); } document = _documentRepository.SaveOrUpdate(document); _documentRepository.DbContext.CommitChanges(); } } catch { _documentRepository.DbContext.RollbackTransaction(); throw; } } return MapperFactory.GetDocumentDto(document); } 这是我的测试代码 [Test] public […]

MOQ – 模拟MVC控制器的Response.Cookies.Clear()

我是MOQ的新手,但我正在使用NUnit进行unit testing。 我已经模拟了我的控制器的所有部分,除了以下行抛出“对象未设置为对象的实例”错误消息。 Response.Cookies.Clear(); 我有以下扩展方法来模拟控制器上下文,它适用于我迄今为止所遇到的所有其他事情(非常感谢此论坛上的好人)。 public static int SetUpForTest(this System.Web.Mvc.Controller ctl, string username, TestingUtility.Roles role) { var routes = new RouteCollection(); MvcApplication.RegisterRoutes(routes); var request = new Mock(MockBehavior.Strict); request.SetupGet(x => x.ApplicationPath).Returns(“/”); request.SetupGet(x => x.Url).Returns(new Uri(“http://localhost/a”, UriKind.Absolute)); request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection()); var response = new Mock(MockBehavior.Strict); response.Setup(x => x.ApplyAppPathModifier(Moq.It.IsAny())).Returns((String url) => url); // response.SetupGet(x => x.Cookies).Returns(new HttpCookieCollection()); […]

nunit测试仅在作为tfs msbuild进程的一部分运行时抛出exception

我正在使用TFS 2012从Visual Studio 2015构建和部署解决方案,没有任何问题。 我决定将我的unit testing作为构建过程的先决条件的一部分。 独立于msbuild进程,unit testing运行没有问题并成功; 但是,当我将它们作为构建过程的一部分合并时,我在构建中遇到以下exception: Exception NUnit.Core.UnsupportedFrameworkException, Exception thrown executing tests in D:\Builds\4\PA1111CE\Dev1111dBus\bin\mmmTests.dll No test is available in D:\Builds\4\PA1111CE\Dev1111dBus\bin\mmmTests.dll. Make sure that installed test discoverers & executors, platform & framework version settings are appropriate and try again. 构建部分成功。 为了确保运行测试,我将Process中的Disable Tests属性设置为false: 除此之外,我还为自动化测试设置了选项: 唯一可用的测试跑步者如下: 另外,根据我的扩展和更新,它显示我确实安装了nunit适配器3: 我究竟做错了什么? 为什么我的构建仅部分成功?

Moq.Mock – 如何设置一个表达式的方法

我正在模拟我的存储库接口,我不知道如何设置一个接受表达式并返回一个对象的方法? 我正在使用Moq和NUnit 接口: public interface IReadOnlyRepository : IDisposable { IQueryable All() where T : class; T Single(Expression<Func> expression) where T : class; } 使用IQueryable进行测试已经设置,但不知道如何设置T Single: private Moq.Mock _mockRepos; private AdminController _controller; [SetUp] public void SetUp() { var allPages = new List(); for (var i = 0; i < 10; i++) { allPages.Add(new Page { Id […]

使用另一个经过测试的function来为实际测试做准备是一种好的unit testing方法吗?

我正在尝试使用NUnit进行unit testing。 目前,我正在编写一个简单的测试来熟悉语法和unit testing的方法。 但我不确定我是否通过以下测试做得对: 被测试的类包含一个包含水果名称的字符串列表,其中可以通过class_under_test.addNewFruit(…)添加新的水果名称。 因此,为了测试addNewFruit(…)的function,我首先使用该方法将新字符串添加到列表中(例如“Pinapple”),并在下一步中validation列表是否包含此新字符串。 我不确定这是否是测试方法function的好方法,因为我依赖于另一个函数的响应(我已经在之前的unit testing中测试过)。 这是测试此function的方法,还是有更好的解决方案? public void addNewFruit_validNewFruitName_ReturnsFalse() { //arrange string newFruit = “Pineapple”; //act class_under_test.addNewFruit(newFruit); bool result = class_under_test.isInFruitList(newFruit); //assert Assert.That(!result); }