Tag: unit testing

Visual Studio中的测试单独成功,在一组中失败

当我在Visual Studio中单独运行我的测试时,它们都会毫无问题地通过。 然而,当我立刻运行所有这些时,一些通过而一些失败。 我尝试在每种测试方法之间暂停1秒但没有成功。 有任何想法吗? 在此先感谢您的帮助…

我如何访问asp.net 5中的内部

在asp.net 5之前,我会在AssemblyInfo.cs中添加“internalsVisibleTo(some.namespace.name)” – 但我的WebApi项目中不再有assemblyInfo.cs。 如何将WebAPI项目中的内部暴露给我的unitTest项目?

ASP.NET MVC控制器unit testing – UrlHelper扩展问题

尝试在我的ASP.NET MVC 3 Web应用程序中进行一些控制器unit testing。 我的测试是这样的: [TestMethod] public void Ensure_CreateReviewHttpPostAction_RedirectsAppropriately() { // Arrange. var newReview = CreateMockReview(); // Act. var result = _controller.Create(newReview) as RedirectResult; // Assert. Assert.IsNotNull(result, “RedirectResult was not returned”); } 很简单。 基本上测试[HttpPost]动作以确保它返回RedirectResult (PRG模式)。 我没有使用RedirectToRouteResult因为没有任何重载支持锚链接。 继续。 现在,我正在使用Moq来模拟Http Context,包括服务器变量,控制器上下文,会话等。到目前为止一切顺利。 直到我在我的动作方法中点击这一行: return Redirect(Url.LandingPageWithAnchor(someObject.Uri, review.Uri); LandingPageWithAnchor是一个自定义HTML帮助器: public static string LandingPageWithAnchor(this UrlHelper helper, string uri1, string uri2) […]

FluentAssertions:排序列表的等价性

我正在尝试使用C#中的FluentAssertions建立两个列表的等价,其中两件事很重要: 元素通过它们持有的值进行比较,而不是通过引用进行比较(即它们是等价的,不相等) 列表中元素的顺序很重要 FluentAssertions(甚至是NUnit)中没有这样做的function吗? 干杯!

先模拟一个方法而不先嘲笑一个类

我正在使用moq4来模拟我的UnitTests中的东西。 我有一个类说TestClass ,其中一个名为TestMethod的方法是我想要测试的方法。 所以我的问题是我的TestMethod需要检查我的TestClass中的testlist 。 像这样:- public Class TestClass { public readonly ISomeService _someService; public bool TestProperty { get; private set; } public List testlist { get; private set; } Public TestClass(ISomeService someService) { _someService = someService; } public async Task TestMethod(string sd) { if(TestProperty) // here how can i control this property in UnitTest. […]

如何使用NUnit和Rhino Mocks模拟HttpContext.Current.Items

我正在使用NUnit和RhinoMocks进行(WebApi)项目的unit testing。 我正在尝试编写一个测试方法,它应该向HttpContext.Current.Items添加一个项目。 public override void OnActionExecuting(HttpActionContext actionContext) { HttpContext.Current.Items.Add(“RequestGUID”, Guid.NewGuid()); base.OnActionExecuting(actionContext); } 我不知道在测试方法中运行时如何使HttpContext.Current.Items可用于该方法。 我怎样才能做到这一点? 另外,如何检查项目是否已添加(可以/应该使用哪种断言)

在unit testing中模拟SignalR Clients.Group的方法

我正在为SignalR应用程序编写模拟测试用例。 我刚开始在unit testingSignalR应用程序的帮助下,但我的要求与那里显示的示例略有不同。 以下是我在谷歌搜索后所做的代码。 SignalRHub public class HubServer : Hub { [HubMethodName(“SendNofication”)] public void GetNofication(string message) { try { Clients.Group(“Manager”).BroadCast(message); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } unit testing public interface IClientContract { } [TestMethod] public void GetNoficationTest() { var hub = new HubServer(); var mockClients = new Mock<IHubCallerConnectionContext>(); hub.Clients = mockClients.Object; var […]

.Net中的UnitTesting属性?

我正在开发一个我想在开源中发布的lib。 我已经开始为代码编写测试,我想知道我是如何测试.Net对象中的属性的。 可以说我有以下内容: public class Person{ #region variables private string _name = String.Empty; private string _surname = String.Empty; #region properties public string Name{ get{ return _name; } } public string Surname{ get{ return _surname; } set{ _surname = value; } } } 我有两个与代码相关的问题: 如何对仅具有getter的Property进行unit testing(在示例中为Name) 如何使用setter和getter对属性进行unit testing(如示例中的Surname) 我想测试那些简单的属性,因为我已经在其他代码中发现错误,Itellinsense做错了自动完成,并且属性没有返回正确的变量。 更新: 我不是在谈论简单的属性,如示例中的那些,它们确实有一些逻辑,并且很难调试。 编写一个使用setter测试getter的测试,反之亦然,因为如果出现故障,我不知道应该指责哪种方法。 我正在使用属性,因为它们被添加为公共变量,后来需要添加更多逻辑。

使用带有NSubstitute的DbSet 和IQueryable 操作对象会返回错误

我想使用NSubstitute通过模拟DbSet对Entity Framework 6.x进行unit testing。 幸运的是, Scott Xu使用Moq提供了一个很好的unit testing库EntityFramework.Testing.Moq 。 因此,我修改了他的代码以适合NSubstitute并且它一直看起来很好,直到我想测试DbSet.Add() , DbSet.Remove()方法。 这是我的代码位: public static class NSubstituteDbSetExtensions { public static DbSet SetupData(this DbSet dbset, ICollection data = null, Func find = null) where TEntity : class { data = data ?? new List(); find = find ?? (o => null); var query = new InMemoryAsyncQueryable(data.AsQueryable()); […]

这是使用和测试使用工厂模式的类的正确方法吗?

我没有很多工厂模式的经验,我遇到过一个我认为有必要的情况,但我不确定我是否正确实施了模式,我担心它的影响我的unit testing的可读性。 我创建了一个代码片段,它近似(从内存中)我正在工作的场景的本质。 我真的很感激,如果有人可以看看它,看看我做了什么似乎是合理的。 这是我需要测试的类: public class SomeCalculator : ICalculateSomething { private readonly IReducerFactory reducerFactory; private IReducer reducer; public SomeCalculator(IReducerFactory reducerFactory) { this.reducerFactory = reducerFactory; } public SomeCalculator() : this(new ReducerFactory()){} public decimal Calculate(SomeObject so) { reducer = reducerFactory.Create(so.CalculationMethod); decimal calculatedAmount = so.Amount * so.Amount; return reducer.Reduce(so, calculatedAmount); } } 以下是一些基本的界面定义…… public interface ICalculateSomething { […]