Mocking Guid.NewGuid()

后端 未结 2 508
天涯浪人
天涯浪人 2021-01-18 02:02

Suppose I have the following entity:

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public Guid UserGuid {          


        
2条回答
  •  隐瞒了意图╮
    2021-01-18 02:38

    If you are using Moq, you can use:

    mockGuidService.SetupSequence(gs => gs.NewGuid())
        .Returns( ...some value here...)
        .Returns( ...another value here... );
    

    I suppose you could also do the following:

    mockGuidService.Setup(gs => gs.NewGuid())
        .Returns(() => ...compute a value here...);
    

    Still, unless you are just supplying a random value within the return function, knowledge of order still seems to be important.

提交回复
热议问题