xunit测试以查看列表中是否存在特定值

问题描述 投票:0回答:1

我正在编写X单元测试。

我要检查列表是否包含新添加的值。

我尝试了下面的代码,但没有得到结果

_record.Add(new PortfolioCompanyLinkModel { Id = 3, PortfolioCompanyId = 1, URL = "www.historiclreports2.com", LinkName = "Historical Reports", ToBeDeleted = false, IsExternalLink = false, LinkId = 1 });
_record.Add(new PortfolioCompanyLinkModel { Id = 4, PortfolioCompanyId = 1, URL = "www.SalesForce.com", LinkName = "SalesForce", ToBeDeleted = false, IsExternalLink = false, LinkId = 2 });
var repo = new PortfolioCompanyLinkRepository(dbContext, obj.HttpContextAccessor);
var update = await repo.GetOne(2);
_record.Add(new PortfolioCompanyLinkModel { Id = update.Id, PortfolioCompanyId = update.PortfolioCompanyId, URL = "www.historiclreportstest.com", LinkName = update.Link.Name, ToBeDeleted = false, IsExternalLink = true, LinkId = update.LinkId });
var delete = await repo.GetOne(1);
_record.Add(new PortfolioCompanyLinkModel { Id = delete.Id, PortfolioCompanyId = delete.PortfolioCompanyId, URL = delete.URL, LinkName = delete.Link.Name, ToBeDeleted = true, IsExternalLink = delete.Link.IsExternalLink, LinkId = delete.LinkId });
await repo.AddUpdateDelete(_record);
await repo.SaveAsync();

var actual = await repo.GetAll();
Assert.Collection(actual, item => Assert.Contains("www.historiclreports2.com", item.URL));

如何检查实际的URL是否具有www.historiclreports2.com?

c# xunit
1个回答
0
投票

为了执行此操作,您希望选择要声明的记录。可以通过多种方式完成此操作,但是我执行此操作的方式始终如下:

var recordInQuestion = actual.Where(x => x.URL .Equals("www.historiclreports2.com")) .SingleOrDefault(); Assert.NotNull(recordInQuestion);

其他选项是:

Assert.Single(actual.Where(x => x.URL.Equals("www.historiclreports2.com")));

Assert.True(actual.Any(x => x.URL.Equals("www.historiclreports2.com")))

将记录分配给变量的SingleOrDefault()与第一个选项一起使用,使您可以轻松地对该对象的数据执行多个断言。

热门问题
推荐问题
最新问题