简体   繁体   中英

How to increment an int every time I run my tests

I'm testing my methods of one database but I have a problem with my Insert test because my object has an autoincrement ID which is created by default when insert the object BUT also it has a name which has to be unique, so the first time I run my test it works perfectly but no more. I though about put an int i = 0 and then increment it and interpolate it into my string but it is reset every time I run my test so it doesn't work. Any ideas?

This is my test:

public async Task CanCreateStringSignal()
{
    var s = new SignalsDTO()
    {
        Name = "Signal",
        TagType = "String",
        Description = "Foo"
    };
    
    var idReturn = await _handler.Create(s);
    var signal = await _handler.GetSignalsByName("Signal");

    Assert.IsNotNull(signal);
    Assert.IsInstanceOfType(signal, typeof(SignalsDTO));
    Assert.AreEqual("Signal", signal.Name);
    Assert.AreEqual(idReturn, signal.IDTag);
}

Just append some GUID and you'll be fine:

Name = "Signal" + Guid.NewGuid()

Now, on each run, now and in 100 years, the name will be unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM