Tag: unit testing

unit testing中的模型状态validation

我正在为这样的控制器编写unit testing: public HttpResponseMessage PostLogin(LoginModel model) { if (!ModelState.IsValid) return new HttpResponseMessage(HttpStatusCode.BadRequest); } 该模型看起来像: public class LoginModel { [Required] public string Username { set; get; } [Required] public string Password { set; get; } } 然后我有像这样的unit testing: [TestMethod] public void TestLogin_InvalidModel() { AccountController controller = CreateAccountController(); … var response = controller.PostLogin(new LoginModel() { }); Assert.AreEqual(HttpStatusCode.BadRequest, […]

UWP Apps中的模拟框架

我试图为Unittest我的UWP应用找到一个好的模拟框架,似乎所有好的Mocking基础设施(MOQ,RhinoMocks等)都可以理解地依赖于UWP不支持的动态代理。 看起来MOQ知道这个限制: https : //github.com/Moq/moq4/issues/195 在这种情况下,微软的帮助不大: https : //social.msdn.microsoft.com/Forums/en-US/652b9d16-c4ab-401c-9239-0af01108e460/uwp-is-there-any-indication-that-窗口-10- UWP的通用的应用程序意志支持码发射?论坛= wpdevelop 是否有任何Mocking基础设施用于unit testingUWP应用程序? 或者任何想法,如果动态代理支持即将在不久的将来为UWP提供? 谢谢!

entity framework核心:记录单个数据库上下文实例的查询

使用EF Core(或任何ORM)我想跟踪ORM在我的软件中进行某些操作期间对数据库的查询次数。 我之前在Python下使用过SQLAlchemy,在这个堆栈上,这很容易设置。 我通常有一个unit testing,它针对一个场景的查询数量,针对内存中的SQLite数据库进行断言。 现在我想使用EF Core做同样的事情,并查看了Logging文档 。 在我的测试设置代码中,我按照文档说的那样做: using (var db = new BloggingContext()) { var serviceProvider = db.GetInfrastructure(); var loggerFactory = serviceProvider.GetService(); loggerFactory.AddProvider(new MyLoggerProvider()); } 但我遇到的问题我怀疑是以下结果(也来自文档): 您只需要使用单个上下文实例注册记录器。 注册后,它将用于同一AppDomain中上下文的所有其他实例。 我在测试中看到的问题表明我的记录器实现是在多个上下文中共享的(这与我阅读它们时的文档一致)。 并且因为a)我的测试运行器在并行运行测试而b)我的整个测试套件创建了数百个db上下文 – 它不能很好地工作。 问题/问题: 我想要的是什么? 即我可以使用仅用于该db上下文实例的db上下文注册记录器吗? 还有其他方法可以完成我想要做的事情吗?

寻找*小*,开源,c#项目与广泛的unit testing

(我问过这个问题,但没有得到很多答复。建议我就C#提出同样的问题。) 我是一名具有很少C#经验的VB.NET开发人员(是的,我知道我需要在C#中编写更多内容),寻找能够学习高unit testing覆盖率的小型开源项目。 我正在寻找小项目,因为我不想花费大量代码来更好地理解如何在我自己的情况下应用unit testing,其中我主要编写的是我内部使用的IT业务应用程序。公司。 更新:让我询问这个​​问题的原始问题就在这里

升级到版本3.9后无法启动Selenium IE Driver

从这里更新到IEDriverServer 3.9 for 64-bit后,我在尝试使用IE运行任何测试时遇到以下exception(版本:11.0.9600.17842,Win8.1)。 有谁知道怎么解决这个问题,还是应该降级到3.8? OpenQA.Selenium.WebDriverException:抛出了一个带有null响应的exception,向URL http:// localhost:53379 / session发送HTTP请求到远程WebDriver服务器。 exception的状态是ReceiveFailure,消息是:基础连接已关闭:接收上发生意外错误。 —-> System.Net.WebException:基础连接已关闭:接收上发生意外错误。 —-> System.IO.IOException:无法从传输连接读取数据:远程主机强制关闭现有连接。 —-> System.Net.Sockets.SocketException:远程主机TearDown强制关闭现有连接:System.NullReferenceException:未将对象引用设置为对象的实例。 堆栈跟踪如下: 结果StackTrace:在OpenQA.Selenium的OpenQA.Selenium.Remium.DriverServiceCommandExecutor.Execute(Command commandToExecute)中的OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)中的OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(命令commandToExecute)。在OpenQA.Selenium的OpenQA.Selenium.Remium.RemoteWebDriver..ctor(ICommandExecutor commandExecutor,ICapabilities desiredCapabilities)的OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)上的Remote.RemoteWebDriver.Execute(String driverCommandToExecute,Dictionary`2参数)位于OpenQA.Selenium.IE.InlennetExplorerDriver..ctor()上的.IE.InternetExplorerDriver..ctor(InternetExplorerDriverService服务,InternetExplorerOptions选项)OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest中System.Net.HttpWebRequest.GetResponse()的WebException (HttpRequestInfo requestInfo) – System.Net.Sockets.NetworkStream.Read上的–IOException(Byte []缓冲区,Int32偏移量, System32.Net.Connection.SyncRead上的System.Net.PooledStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)的Int32大小)(HttpWebRequest请求,Boolean userRetrievedStream,Boolean probeRead) – System.Net.Sockets上的SocketException System.SNet.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)的.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32大小,SocketFlags socketFlags)

unit testing – 让unit testing调用其他unit testing是不好的forms

我有一个名为TestMakeAValidCall()的unit testing。 它测试我的手机应用程序拨打有效电话。 我即将编写另一个名为TestShowCallMessage()的测试,该测试需要为测试进行有效调用。 在该测试中调用TestMakeAValidCall()是不好的forms? 作为参考,这是我的TestMakeAValidCall()测试。 [TestMethod] public void TestMakeAValidCall() { //Arrange phone.InCall = false; phone.CurrentNumber = “”; // Stub the call to the database data.Expect(x => x.GetWhiteListData()). Return(FillTestObjects.GetSingleEntryWhiteList()); // Get some bogus data string phoneNumber = FillTestObjects.GetSingleEntryWhiteList(). First().PhoneNumber; // Stub th call to MakeCall() so that it looks as if a call was made. […]

如何使用提示unit testingMicrosoft bot对话框

我正在使用Microsofts Bot Framework,我正在尝试单独测试(单独)我拥有的Dialog: public class MyDialog : IDialog { public async Task StartAsync(IDialogContext context) { PromptDialog.Confirm(context, MessageReceived, “Are you sure?”, “Sorry what was that?”); } private async Task MessageReceived(IDialogContext context, IAwaitable result) { bool isSure = await result; string response = isSure ? “Awesome” : “Sorry”; IMessageActivity messageActivity = context.MakeMessage(); messageActivity.Text = response; await context.PostAsync(messageActivity); […]

测试使用HttpContext.Current.Request.Files的Web API方法?

我正在尝试编写一个使用HttpContext.Current.Request.Files的Web API方法的测试,经过详尽的搜索和实验后,我无法弄清楚如何模拟它。 正在测试的方法如下所示: [HttpPost] public HttpResponseMessage Post() { var requestFiles = HttpContext.Current.Request.Files; var file = requestFiles.Get(0); //do some other stuff… } 我意识到还有其他 类似的 问题 ,但它们没有解决这个具体情况。 如果我试图模拟上下文,我会遇到Http*对象层次结构的问题。 假设我设置了各种模拟对象(使用Moq ),如下所示: var mockFiles = new Mock(); mockFiles.Setup(s => s.Count).Returns(1); var mockFile = new Mock(); mockFile.Setup(s => s.InputStream).Returns(new MemoryStream()); mockFiles.Setup(s => s.Get(It.IsAny())).Returns(mockFile.Object); var mockRequest = new Mock(); mockRequest.Setup(s => […]

unit testingHtmlHelper扩展方法失败

我正在尝试测试我编写的一些HtmlHelper扩展方法。 我的第一个问题是如何创建一个HtmlHelper实例,但我使用此代码解决了这个问题: private static HtmlHelper CreateHtmlHelper(T model) { var viewDataDictionary = new ViewDataDictionary(model); var controllerContext = new ControllerContext(new Mock().Object, new RouteData(), new Mock().Object); var viewContext = new ViewContext(controllerContext, new Mock().Object, viewDataDictionary, new TempDataDictionary(), new Mock().Object); var mockViewDataContainer = new Mock(); mockViewDataContainer.Setup(v => v.ViewData).Returns(viewDataDictionary); return new HtmlHelper(viewContext, mockViewDataContainer.Object); } 我的几个测试现在工作正常,但有一个测试会引发exception。 测试定义如下: // Arrange var inputDictionary = […]

模拟AsNoTrackingentity framework

我如何模拟AsNoTracking方法? 在下面的示例中,DbContext已注入服务类。如果我从GetOrderedProducts方法中删除AsNoTracking扩展方法,但是AsNoTracking测试失败,因为它返回null,它工作正常。 我也尝试过模拟AsNoTracking来返回正确的值,但它没有用。 public interface IUnitOfWork { IDbSet Set() where TEntity : class; int SaveAllChanges(); } public class Entites : DbContext, IUnitOfWork { public virtual DbSet Products { get; set; } // This is virtual because Moq needs to override the behaviour public new virtual IDbSet Set() where TEntity : class // This is virtual […]