XUnit ThrowsAsync<>似乎没有检查异常类型。
我正在尝试检查单元测试中抛出的异常的类型。然而,XUnit似乎并不是不检查异常类型,而是只检查它是一个异常。
当然,下面的测试会失败,因为抛出的异常是NotImplementedException类型的,但是测试期待的是ArgumentException。只要抛出任何类型的异常,它就会过去。
public class AssertThrowsTests
{
[Fact]
public void Tests()
{
// Using a method as a delegate
我有一个简单的集线器,我试图用FakeItEasy编写一个测试,并且调用客户端的验证没有通过。我有一个使用MOQ和XUnit的单独项目的示例。
public interface IScheduleHubClientContract
{
void UpdateToCheckedIn(string id);
}
public void UpdateToCheckedIn_Should_Broadcast_Id()
{
var hub = new ScheduleHub();
var clients = A.Fake<IHubCallerConnectionConte
在下面的示例中,FakeItEasy返回0,尽管IThing成员GetValue()尚未定义。我的问题是:为什么从未定义的成员调用中返回的0值而不是抛出的异常;是否存在某种通用的嘲笑/伪造/顽固框架软件模式,它规定为调用未定义的成员抛出异常是否定的?
public interface IThing
{
int GetValue();
}
public class Thing: IThing
{
public int GetValue()
{
return 1000;
}
}
[TestMethod]
public void T
我想知道如何创建测试(使用APICore2.2API中的xUnit )来检查我的.NET端点是否抛出了特定的异常。
这就是我到目前为止所知道的:
[Theory]
[InlineData("/values/sample")]
public async Task Sample_WhenCreatingInvalidSampleData_ThenExceptionIsThrown(string url)
{
// given
var command = new AddSampleCommand { Name = "test" };
// w
尝试创建用于测试目的的URLHelper会引发NullReferenceException。
示例:
[Fact]
public async void AuthenticateAsyncTest()
{
// Arrange
var controller = new Controller(serviceProvider)
{
Url = new UrlHelper(new ActionContext()) // Exception thrown
};
// Act
var result = await controller.Authenticate() as
我有一个异步方法如下所示:
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的新手。我在代码中抛出一个新的异常:
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);
我有以下测试失败的代码:
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
我正在C#中使用F#编写测试,使用xUnit和FsCheck编写钻石卡塔,在试图检查用户输入无效的情况下是否抛出异常时遇到了一些困难(任何字符都不是没有任何对话框的字母)。下面是代码现在的样子:
正在测试的方法:
public static string Make(char letter)
{
if (!Regex.IsMatch(letter.ToString(), @"[a-zA-Z]"))
{
throw new InvalidOperationException();
}
// code that makes the d
我目前有一个集成测试,可以执行一些操作,例如:
var link = await Blah();
偶尔,Blah()会抛出一个异常。我想记录异常,如果它符合某一类型,我想通知用户一个常见的潜在修复。
我目前的方法是尝试/捕捉,但我不确定:
XUnit推荐的向用户输出的方法是什么?我猜Console.WriteLine在这里不太好吧?
有比尝试/捕捉更干净的方法吗?我仍然需要link值。
在编写单元测试用例以测试抛出的异常时,尝试使用以下两种方法使用xUnit
Assert.Throws(行动)
Record.Exception(行动)
以下是代码
public class Me : Entity, IAggregateRoot
{
public string Name {get; }
private List<Friend> friends;
public IReadonlyCollection<Friend> Friends => friends;
public Me(string name)
我已经实现了使用FakeItEasy框架模拟测试公共虚拟方法的解决方案。下面我有一个带有私有方法的Test类,而私有方法不能是虚拟的。因此,请帮助我模拟使用FakeItEasy框架的私有方法
待测试的类
public class XYZ
{
public static int nValue = 0;
private void AddInetegers()
{
int i = 3;
int j = i * 100;
int k = j * 30 / 100;
Show(String.Format(
可能重复:
我想知道的是,如何在C#单元测试中断言异步方法抛出异常?我能够在Visual 2012中用Microsoft.VisualStudio.TestTools.UnitTesting编写异步单元测试,但还没有弄清楚如何测试异常。我知道xUnit.net也以这种方式支持异步测试方法,尽管我还没有尝试过这个框架。
为了说明我的意思,下面的代码定义了被测试的系统:
using System;
using System.Threading.Tasks;
public class AsyncClass
{
public AsyncClass() { }
public