我是Xunit的新手。我在代码中抛出一个新的异常:
throw new NotImplementedException("Missing UPC. File is corrupt");
在我的测试文件中,我写道:
[Fact]
public void ParserRow_Throws_Exception_For_Null_UPC()
{
var record = GetRessoRecord();
record.UPC = "";
RevenueReportItem result = Sut.ParseRow(record);
我想知道如何创建测试(使用APICore2.2API中的xUnit )来检查我的.NET端点是否抛出了特定的异常。
这就是我到目前为止所知道的:
[Theory]
[InlineData("/values/sample")]
public async Task Sample_WhenCreatingInvalidSampleData_ThenExceptionIsThrown(string url)
{
// given
var command = new AddSampleCommand { Name = "test" };
// w
我目前有一个集成测试,可以执行一些操作,例如:
var link = await Blah();
偶尔,Blah()会抛出一个异常。我想记录异常,如果它符合某一类型,我想通知用户一个常见的潜在修复。
我目前的方法是尝试/捕捉,但我不确定:
XUnit推荐的向用户输出的方法是什么?我猜Console.WriteLine在这里不太好吧?
有比尝试/捕捉更干净的方法吗?我仍然需要link值。
XUnit ThrowsAsync<>似乎没有检查异常类型。
我正在尝试检查单元测试中抛出的异常的类型。然而,XUnit似乎并不是不检查异常类型,而是只检查它是一个异常。
当然,下面的测试会失败,因为抛出的异常是NotImplementedException类型的,但是测试期待的是ArgumentException。只要抛出任何类型的异常,它就会过去。
public class AssertThrowsTests
{
[Fact]
public void Tests()
{
// Using a method as a delegate
代码测试中的一些错误引起了这个问题,由于代码的扩展,这里是一个完整的项目,它准确地复制了我们的问题(为了省去你分析成吨的行)。
问题出在试图从异步方法捕获异常时,这里是AsyncTest.cs文件:
using System;
using Xunit;
namespace AsyncTest
{
public class AsyncTest
{
[Fact]
public void ProbeTest()
{
// Arrange
var probe = new Probe();
我在使用xunit.assert.dll验证Excel文件时遇到这个错误(“异常抛出:xunit.assert.dll中的'Xunit.Sdk.EqualException‘”)。我想使用#验证excel文件,而我使用的是DocumentFormat.OpenXML
using System;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
usin
我的ASP.NET Core3API的BDD风格的单元测试出错了。引用的包(xunit:2.4.1,xbehave:2.4.1,xunit.runner.viewalstudio:2.4.2)。
下面是一个简化的示例测试,它会导致相同的错误:
[Fact]
public void Test1()
{
int x=0,
y=0,
sum = 0;
"Given x = 20".x(() => x = 20);
"And y = 10".x(() => y = 10);
"Wh
我有一个异步方法如下所示:
public async IAsyncEnumerable<IEnumerable<Foo>> Load()
{
//some times throws exception in this line
var responseEnumerable = _crawler.Crawl();
await foreach (var response in responseEnumerable)
{
yield return SomWorks(response
我是XUnit和Moq的新手。我有一个将字符串作为argument.How来使用XUnit处理异常的方法。
[Fact]
public void ProfileRepository_GetSettingsForUserIDWithInvalidArguments_ThrowsArgumentException() {
//arrange
ProfileRepository profiles = new ProfileRepository();
//act
var result = profiles.GetSettingsForUserID("")
我试图对返回IActionResult的控制器进行单元测试,但在某些情况下也可以抛出异常。我遇到的问题是,我不知道如何调用它,因为断言会抛出一个错误,您无法将其从IActionResult转换为Action。
我将如何进行下面的测试声明?
Assert.Throws<Exception>(await controller.SendEmail(email)); //how to test this
我翻阅了微软的测试控制器文档,没有发现相关的东西。我看到的大多数测试异常的例子都是针对访问存储库或服务之类的。
据我所知,我可以返回一个错误的请求或重定向到页面的错误信息。但我想要达到的
我有一个在某些情况下抛出SerializationException的ASP.NET Web API web服务。问题是我无法在服务器端捕获和记录这个异常--它唯一出现的地方是客户端的HTTP响应的主体中。
我按照中的描述注册了一个ExceptionFilterAttribute,并验证了当我在控制器中抛出异常时,它是否工作正常。不幸的是,SerializationException在响应过程中被抛出了(在控制器之后),似乎完全被ASP.NET吞没了。我也试过在Global.asax.cs中挂钩Application_Error(),但它也没有出现在那里。
如何在Web API响应期间捕获Se
我想知道如何为ASP.NET Web API控制器抛出的异常编写文档注释?问题是,通常你会为每种情况抛出一种不同类型的异常,并因此为每种异常类型编写一个文档注释,例如/// <exception cref="ResourceNotFound">Resource not found</exception>。但是,对于HttpResponseException,异常的StatusCode属性用于标识错误情况。
我应该如何记录可能抛出HttpResponseException的每种情况,每种情况都由一个状态代码标识,对应于您如何为每个异常类型编写/// <
我有以下测试失败的代码:
open Xunit
open FsUnit.Xunit
let rec openOrSenior xs =
match xs with
| head :: tail when fst head >= 55 && snd head >= 7 -> "Senior" :: openOrSenior tail
| head :: tail -> "Open" :: openOrSenior tail
| [] -> []
[<Fact>]
let
System.NotImplementedException: The method or operation is not implemented.
at Microsoft.Web.Administration.Interop.IAppHostMethodInstance.Execute()
at Microsoft.Web.Administration.ConfigurationMethodInstance.Execute()
at Microsoft.Web.Administration.ConfigurationElement.ExecuteMethod(Strin
我正在尝试重写ASP.NET 4.6在ASP.NET核心中的一些授权。
我知道授权已经发生了一些变化,我发现很难在ASP.NET核心中实现我非常简单的auth策略。
My requirements:
对服务器的每个请求都应该包括一个名为"key“的标题。根据该键的值,我将能够查询数据库并检查该密钥是代表常规用户还是管理用户。如果请求不包含有效密钥,则请求未被授权。
我将如何在ASP.NET核心中实现这一点?我发现的每一个例子似乎对我的需求来说都太过分了。
在ASP.NET 4.6中,我在控制器上使用了自己的自定义AuthorizeAttributes。
[User]
public IH