这是我对Asp.Net Web应用程序的第一次测试.我们有一个由几个模块组成的引擎.我需要在Engine Module中测试类.虽然这些条款是Asp.Net App的一部分,但它们只包含业务逻辑.
我如何孤立地测试这些类,而不是WebApp的一部分?因为我收到了这个错误
Web请求' http:// localhost:8936 / '在未运行测试的情况下成功完成.当配置Web应用程序以进行测试失败(处理请求时发生ASP.NET服务器错误),或者没有执行ASP.NET页面时(URL可能指向HTML页面,Web服务或目录列表).在ASP.NET中运行测试需要URL解析为ASP.NET页面,并且页面要正确执行到Load事件.来自请求的响应存储在文件'WebRequestResponse_BlogManagerBPOConstr.html'中,带有测试结果; 通常可以使用Web浏览器打开此文件以查看其内容.
谢谢
编辑: @Mark,这是设计师生成的TestMethods之一
/
// <summary>
///A test for BlogManagerBPO Constructor
///</summary>
// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
// http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
// whether you are testing a page, web service, or a WCF service.
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("D:\\WorkingCopies\\MyProject\\Engine", "/")]
[UrlToTest("http://localhost:8936/")]
public void BlogManagerBPOConstructorTest()
{
BlogManagerBPO target = …Run Code Online (Sandbox Code Playgroud) 我最近安装了VS 2010 Ultimate,并决定将项目移至TeamCity,该项目包含一些内部单元测试的*.dll.
在VS 2008中,在构建运行阶段的设置过程将包括:
在VS2010中,前面提到的dll文件已经消失了,我无法设置MSTests,有没有人有配方怎么做?
编辑:用*.trx文件行替换配置文件
我目前正在尝试测试使用TryUpdateModel()的插入方法.我正在伪装控制器上下文,这是必要的,虽然它的工作原理似乎并没有张贴我设置的模型.
这是我正在测试的方法:
[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _SaveAjaxEditing(int? id)
{
if (id == null)
{
Product product = new Product();
if (TryUpdateModel(product))
{
//The model is valid - insert the product.
productsRepository.Insert(product);// AddToProducts(product);
}
}
else
{
var recordToUpdate = productsRepository.Products.First(m => m.ProductID == id);
TryUpdateModel(recordToUpdate);
}
productsRepository.Save();
return View(new GridModel(productsRepository.Products.ToList()));
}
Run Code Online (Sandbox Code Playgroud)
这是我目前的测试:
[TestMethod]
public void HomeControllerInsert_ValidProduct_CallsInsertForProducts()
{
//Arrange
InitaliseRepository();
var httpContext = CustomMockHelpers.FakeHttpContext();
var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
controller.ControllerContext = context;
//controller.ControllerContext = new ControllerContext();
var …Run Code Online (Sandbox Code Playgroud) 我想知道,visual studio会生成带有TestContext成员的测试类,并将其实例传递给ClassInitialize方法.但是,你真的使用这门课吗?如果是这样,那么使用它的方便/需要的好例子是什么?
我已经写了一段时间的测试,从来没有真正觉得我需要使用TestContext.你呢?
我有像这样的功能测试的层次结构
[TestClass]
class BaseClass
{
// specific methods and members relevant to all functional tests are here
// ie how to work with db
}
[TestClass]
class Module1:BaseClass
{
[ClassInitialize]
public static void Module1TestsInit(TestContext context)
{
//create some db data here, which is needed only for Module1
}
[ClassCleanup]
public static void Module1TestsCleanup()
{
//delete Module1 db data
}
}
[TestClass]
class Module2:BaseClass
{
[ClassInitialize]
public static void Module2TestsInit(TestContext context)
{
//create some db data here, which is needed only …Run Code Online (Sandbox Code Playgroud) 给定一个TFS生成细节(IBuildDetail)用.Status的PartialSuccess,和.TestStatus中Failed 怎么可能我去取回的已经失败上建立测试(MSTest的)名单?
我有一个工作沙箱,我可以通过SDK联系TFS并检索最新的PartialSuccess构建,但似乎无法找到哪个服务可能有这个单元测试数据以及我如何查询它.
任何人都能解释一下吗?
我有ac #test方法的问题.
看起来像:
public void GetRolesTest()
{
RoleProvider target = new RoleProvider();
string username = "FOO";
string[] expected = new string[2];
expected[0] = "Admin";
expected[1] = "User";
string[] actual;
actual = target.GetRoles(username);
Assert.AreEqual<string[]>(expected, actual);
}
Run Code Online (Sandbox Code Playgroud)
测试的方法只做以下几点:
public override string[] GetRoles(string username)
{
string[] output = new string[2];
output[0] = "Admin";
output[1] = "User";
return output;
}
Run Code Online (Sandbox Code Playgroud)
运行测试后,我收到以下错误:
Error in "Assert.AreEqual". Expected:<System.String[]>. Acutally:<System.String[]>.
Run Code Online (Sandbox Code Playgroud)
可以sombody告诉我那里有什么问题?
我有一个自定义的Windows窗体控件,其中包含MSTest单元测试.我在这个类中添加了一个异步方法,我需要覆盖它,但新测试总是超时.
经过一些实验,我把问题简化为控制创建和等待.
[TestMethod]
public async Task TestMethod1()
{
Control c = new Control();
await Task.Delay(1);
}
Run Code Online (Sandbox Code Playgroud)
我注意到测试从SynchronizationContext.Current设置开始null,然后一旦创建控件,它就会变为a WindowsFormsSynchronizationContext.如果我强制恢复同步上下文null,那么测试将通过.
[TestMethod]
public async Task TestMethod1()
{
Control c = new Control();
SynchronizationContext.SetSynchronizationContext(null);
await Task.Delay(1);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在WindowsFormsSynchronizationContext没有阻塞的情况下使用await ?
An exception occurred while invoking executor 'executor://mstestadapter/v2': Constructor on type 'Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.UnitTestRunner' not found.
public ActionResult View(string name)
{
if (TempData["SessionVariable"] != null)
{
FileName = name;
return View();
}
else
{
return RedirectToAction("index", "Home");
}
}
Run Code Online (Sandbox Code Playgroud)
测试方法
public void UseCaseView_CorrectRequirements()
{
var mock = new Mock<Controller>();
mock.Setup(p => p.TempData["SessionVariable"]).Returns("admin");
Controller.View("SAMPLE.xml");
}
Run Code Online (Sandbox Code Playgroud)
会引发错误,例如
结果StackTrace:
在Moq.Mock.SetupGetPexProtected [T,TProperty] [T,TProperty](
1 mock, ExpressionC中的模拟1表达式,条件条件)下的C:\ projects \ moq4 \ Source \ Mock.cs:line 883处的Moq.Mock.ThrowIfSetupExpressionInvolvesUnsupportedMember(表达式设置,MethodInfo方法):位于Moq.Mock.SetupGet [T,TProperty](模拟1 mock, Expression1表达式,条件条件)处的Moq.Mock.SetupGet [T,TProperty](模拟1表达式,条件条件)处的第537行:\ projects \ moq4 \ Source \ Mock.cs:line 517处Moq.Mock.Setup [T,TResult]1 mock, Expression中的C:\ projects \ moq4 \ Source \ …
mstest ×10
c# ×6
unit-testing ×4
moq ×2
asp.net ×1
asp.net-core ×1
asp.net-mvc ×1
async-await ×1
inheritance ×1
nuget ×1
teamcity ×1
tfs-sdk ×1
webtest ×1
winforms ×1