【c# – 在TDD中创建测试数据的最佳位置在哪里?】教程文章相关的互联网学习教程文章

c# – 在TDD中创建测试数据的最佳位置在哪里?

我使用NUnit集成测试.我正在尝试测试以确保用户无法使用现有电子邮件创建帐户. (test@example.com) 我需要在数据库中有测试数据(使用test@example.com电子邮件帐户). 我可以在测试函数或sql脚本中创建此帐户(并在集成测试之前运行它). 创建此测试数据的最佳位置在哪里?解决方法:这两种选择都没有错,但有很多方法可以扩展和巩固你的策略: > Mocking与TDD齐头并进>使用RedGate’s Sql Data Gen等工具生成db测试数据>创建pluggable ...

c# – Xml Parser类的单元测试【代码】

我有一个类,它使用C#中的XmlReader和XmlReaderSettings类来根据模式验证Xml文件.由于我的应用程序涉及从数据库中读取Xml数据,因此我决定在MessageBox中向用户显示错误.因此,任何验证错误以及抛出的任何异常都将在MessageBox中显示一个字符串“解析时出错”. 我还有一个布尔变量,它返回解析是否成功. 现在,我在Assert中使用Parse函数返回的布尔值,同时让parse函数解析有效和无效的Xml文件. 因此,在运行测试用例套件时,我将这些消息框...

c# – 如何在VS2010的Test-Result窗口中查看单元测试的输出?【代码】

我是单元测试的新手,我想看看测试的输出. 让我们假设我正在测试某些对象的存在:List<MyObject> actual = target.GetMyObjects(); Assert.IsTrue(actual.Count > 0, String.Format("{0} objectes fetched", actual.Count));在VS2010的“测试结果”窗口中,我想看到“String.Format(”{0} objectes fetched“,actual.Count)”的结果.那可能吗?解决方法:找到了:我将列Output(StdOut)添加到Test Result窗口. 我将测试方法的结尾更改为...

c# – 自动测试属性getter / setter【代码】

我们对域对象的许多属性使用支持字段,例如:protected string _firstname;public virtual string Firstname {get { return _firstname; }set { _firstname = value; } }我偶尔会像下面的例子那样制作愚蠢的拼写错误,并希望编写一个验证所有这些属性的测试,而不是手动对每个对象进行测试.public virtual string Firstname {get { return _firstname; }set { _firstname = Firstname; } }是否容易编写或者是否已经存在库以测试这些支...

c# – 在单元测试AAA模式中提取数据【代码】

在“AAA”模式中,应该写出行为数据的提取? 在法案或断言部分? 考虑这个单元测试,两个人的提取,如果它在例如示例中或在断言中?我们想为公司的所有UT制定标准.[Test] public void Test() {// Arrangevar p1 = new Person();var p2 = new Person();Session.Save(p1);Session.Save(p2);// Actvar result = new PersonQuery().GetAll();var firstPerson = result[0];var secondPerson = result[1];// AssertAssert.AreEqual(p1.Id, f...

c# – 尝试测试自定义成员资格提供程序【代码】

我正在尝试测试我的自定义成员资格提供程序,因为它描述为here 在测试时我有System.TypeLoadException:Could not load type 'Domain.WebProviders' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral....,这是我的代码TestBase.cspublic class TestBase{protected string _username;protected string _password;protected string _email;protected CustomMembershipProvider _provider;protected NameValueCollection ...

c# – 如何使用Moq来满足单元测试的MEF导入依赖性?【代码】

这是我的界面public interface IWork {string GetIdentifierForItem(Information information); }和我的班级public class A : IWork { [ImportMany] public IEnumerable<Lazy<IWindowType, IWindowTypeInfo>> WindowTypes { get; set; }public string GetIdentifierForItem(Information information) {string identifier = null;string name = information.TargetName;// Iterating through the Windowtypes // searching the 'Nam...

c# – UnitTest -issue来自mock的测试方法【代码】

我正在使用一个具有void方法的接口的类看起来像这个女巫现在是完全可行的方法:public void SellGivenQuantityOfProduct(Product product, int quantity) {product.StockQuantity = product.StockQuantity - quantity; }但是使用TDD方式,我开始在下面这样做,但计算从未在我的测试方法中发生[TestMethod]public void Can_Sell_Given_Quantity_Of_Single_Products_Stock_Quantity(){var mock = new Mock<ISellBuyPipeLine>();mock.Se...

c# – 有没有办法计算测试方法的耗用时间而忽略初始化时间?【代码】

这是一个踩在StackOverflow和SuperUser之间的灰色区域的问题.由于我怀疑答案可能涉及与代码相关的解决方案,例如创意使用StopWatch,我会在这里提出. 我正在使用Visual Studio 2013对依赖于实体框架数据模型的控制器执行单元测试. Test Explorer很好地在一个简短的列表中显示我的测试结果以及它们的经过时间. 但我发现,我的控制器单元测试所用的时间比我预期的要长得多.我开始怀疑这是由于我用来创建一个模拟的初始化代码(使用Moq,但...

c# – 在具有基类的多个浏览器中运行Selenium测试【代码】

假设我有代码:namespace SeleniumTests {[TestFixture(typeof(FirefoxDriver))][TestFixture(typeof(InternetExplorerDriver))]public class TestWithMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new(){private IWebDriver driver;[SetUp]public void CreateDriver () {this.driver = new TWebDriver();}[Test]public void GoogleTest() {driver.Navigate().GoToUrl("http://www.google.com/");IWebElement qu...

c# – 问题测试私有静态功能【代码】

我在找到私有静态方法时遇到问题.我的老板希望对它进行测试(尽管我知道测试并不是真的有必要). 这是我要测试的代码:namespace Retail_Utilities.Printables{public partial class WarrantyDropoffSlip : INotifyPropertyChanged{ private static string AddBusinessDays(string businessDays){if (string.IsNullOrEmpty(businessDays)) return DateTime.Now.ToShortDateString();var bDays = int.Parse(businessDays);var dat...

c# – 测试覆盖IsValid的ValidationAttribute【代码】

我在测试自定义验证属性时遇到了一些麻烦.由于在我的单元测试中调用IsValid方法时方法签名受到保护,我无法传入Mock< ValidationContext>对象,它调用基础虚拟bool IsValid(对象值). ValidationAttributeprotected override ValidationResult IsValid(object value, ValidationContext validationContext) {var otherPropertyInfo = validationContext.ObjectType.GetProperty(this.otherPropertyName);var otherPropertyValue = oth...

如何在c#中测试视图模型是否处于设计器模式

我是C#和WPF的新手,所以我想从MVVM开始.我有一个小的WPF应用程序,我想测试我的视图模型是否在Designer模式下创建(检查DesignerProperties);鉴于我有一个IDataService,它从硬编码列表(设计时)或REST服务(运行时)向ViewModel提供数据. 有没有办法模拟或存根这个DesignerProperties对象来强制它成为一个或另一个状态? 提前致谢.解决方法:Is there a way to Mock or Stub this DesignerProperties object to forceit to be one or the...

c# – 如何在asp.net mvc核心中编写Identity Server 4的集成测试【代码】

我正在使用身份服务器4的web api.我不知道从哪里开始编写集成测试.我有一个登录控制器接收用户名和密码,用于ResourceOwnerPassword授权类型.以下是我的代码. 控制器.[Route("Authentication/Login")] public async Task<IActionResult> WebApiLogin(string username, string password) {var accessToken = await UserAccessToken.GenerateToken(username, password);return new JsonResult(accessToken); }用于生成令牌的类public ...

c# – 为EF dbcontext设置模拟对象以测试存储库方法【代码】

我有entityframework repo,它从sqldb获取行程信息.我已经创建了repo并在构造函数上注入dbContext并使用该上下文执行数据库操作.public class WorldRepository : IWorldRepository{private WorldContext _context;public WorldRepository(WorldContext context){_context = context;}public void AddSop(string tripName, Stop newStop){var trip = GetTipByName(tripName);if (trip != null){trip.Stops.Add(newStop);_context.Sto...