chancejs / chancejs

Chance - Random generator helper for JavaScript
http://chancejs.com
MIT License
6.46k stars 455 forks source link

The best way to fake uuid.v4 #215

Open hellboy81 opened 8 years ago

hellboy81 commented 8 years ago

In SUT is v4 method uuid used:

uuid = require('uuid')
uuid.v4()
'b14b1abb-180e-4d34-a59c-940bc2e77ac7'

What is the best way fake their returning value in Arrange phase?

// How should we replace 'b14b1abb-180e-4d34-a59c-940bc2e77ac7'
// Can I simply use chance.guid?
uuid = {v4: sandbox.stub().named('uuid.v4').returns(...)}
oliversalzburg commented 8 years ago

Why would you stub a method that generates a UUIDv4 with a method that generates a UUIDv4 when you could just use the method itself?

hellboy81 commented 8 years ago

OK, in which cases I SHOULD do it?

I should test if - char has been removed from random uuid:

// Code in SUT
uuid.v4().replace(/-/g, '')

As I mentioned I should stub uuid.v4() with string constant only? Usage of chance.guid is in this case unnecessary.

oliversalzburg commented 8 years ago

I'm sorry, but I don't think I understand the question :( Maybe someone else can help.

leesei commented 8 years ago

Are you comparing whether you should use uuid.v4() and chance.guid()? They provide the some kind of output and it's up to you to decide which one to use.

jayarjo commented 10 months ago

is chance.guid() guaranteed to produce unique result? Even if called in sequence in for loop? We have a case when it seemingly produces duplicates. It's intermittent, so we cannot reliably reproduce.

oliversalzburg commented 10 months ago

is chance.guid() guaranteed to produce unique result?

No. This is a library for fake data.