gpt4 book ai didi

c# - 如何验证内部属性是否设置了正确的值?

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:39 25 4
gpt4 key购买 nike

这是一段代码:

[HttpPost(UriFactory.FOO_ROUTE)]

public async Task<ActionResult> AddFooAsync([FromRoute]string novelid, [FromBody]AddFoo command)
{
var novel = await RetrieveNovel(novelid);
if (novel == null) return NotFound();
if (!ModelState.IsValid) return BadRequest(ModelState);

command.FooId = Guid.NewGuid();
novel.AddFoo(command);
await _store.SaveAsync(novel);

return Created(UriFactory.GetFooRoute(novel.Novel.NovelId, command.FooId), command);
}

如何在单元测试中验证 FooId 确实设置了 NewGuid?

最佳答案

Typemock Isolator您可以验证内部属性是否设置如下:

  [TestMethod, Isolated]
public void Test1
{
var testFoo = Isolate.Fake.Dependencies<AddFoo>();
var newGuid = new Guid();
testFoo.FooId = Guid.NewGuid()
Isolate.Verify.NonPublic.Property.WasCalledSet(testFoo, "FooId").WithArgument(newGuid);
}

或者您可以提取该属性并断言它是一个 Guid:

  [TestMethod, Isolated]
public void Test2
{
var testFoo = Isolate.Fake.Dependencies<AddFoo>();
var fooID = Isolate.Invoke.Method(testFoo , "getFooID");
Assert.IsTrue(fooID is Guid);
}

关于c# - 如何验证内部属性是否设置了正确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47925230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com