4

I want to generate a random string of - say length 80.

I'm thinking something like Fixture.Create<string>(length: 80); or Fixture.Create<string>(minimumLength: 60, maximumLenth: 100);. Both would work perfectly fine.

Creating a Customization seems overkill, but maybe that's the correct approach. There are plenty of questions regarding getting a shorter string than the default, which can be achieved with substring, but I need a longer one.

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
  • Have you considered asking AutoFixture for more than one string, and then concatenate them together until it has the length you desire? – Mark Seemann Oct 11 '16 at 16:53
  • Also: why do you need this? – Mark Seemann Oct 11 '16 at 16:54
  • Yes I did consider that but thought the intention would be cleaner if I could specify it directly in the API. The reason I want a long string is to assert that my model validation works properly, for example that my `RangeAttribute` kicks in as expected. – Jim Aho Oct 11 '16 at 20:40

4 Answers4

11

Try this:

string.Join(string.Empty,Fixture.CreateMany<char>(stringLength))

stringLength is how long you wanted your generated string to be.

or

By default Fixture.Create<string>() creates a string of length 36.
So if you want a string length more than 36, this code will generate a string of length 72:

string.Join(string.Empty,Fixture.CreateMany<string>(2))  
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Hina Ahuja
  • 126
  • 1
  • 3
1

Check out this question.

This says that StringLength attribute can help generate strings of a given length.

Also there is an article from a library maintainer. In this article he says that the method like eq fixture.Build<MyObject>.With(x = x.MyString, 0, 100); will not be implemented by design considerations.

So if you don't want to use the attribute than you have to run with Customization

Community
  • 1
  • 1
Michael
  • 1,027
  • 10
  • 22
  • Well that question uses `CreateAnonymous` (which is deprecated) and talks about shortening only. I don't see how it helps me? – Jim Aho Oct 11 '16 at 15:29
  • The article says that the method for just making a string will not be created because of the design considerations. Although the article is written in 2012 the fact that there is no such method may say that you will need to use another approach (like you've already said). Also I didn't get the point about longer string: do you need a string of random characters or random length in given range? – Michael Oct 11 '16 at 15:35
  • Random length in a given range, or just a specific length – Jim Aho Oct 11 '16 at 15:50
  • May the `StringLength` attribute help? It is not clear from [this](http://blog.nikosbaxevanis.com/2011/09/18/stringlengthattribute-support-in-autofixture/) announcement will it add new chars to a string but it may help. – Michael Oct 11 '16 at 15:59
  • Maybe - seems you must decorate your non-test code with this attribute to work, and I prefer not to clutter the code with test-related stuff – Jim Aho Oct 11 '16 at 16:04
  • I see. So in that case if somebody will not provide a better advice than it seems that you should have to create Customization. – Michael Oct 11 '16 at 16:16
0

There is no straight-forward way to accomplish this, as already pointed out by Michael.

The closest I could get was to use Fixture.CreateMany<string>(3).Join(""). Thanks to Mark Seemann for the tip.

I just wanted a string more than 50 characters, but didn't care if it was 100 or 300. If I however wanted a precise length I could also call Fixture.CreateMany<string>(3).Join("").Substring(length)

Community
  • 1
  • 1
Jim Aho
  • 9,932
  • 15
  • 56
  • 87
  • Looks like this issue could be addresses by this AutoFixture improvement: https://github.com/AutoFixture/AutoFixture/issues/678. Unfortunately it requires .NET 4.5 so it cannot be accomplished in AF 3.x based on .NET 4.0 – Serhii Shushliapin Oct 12 '16 at 17:16
0
    private Func<int, string> LongStringFactory { get; }

    public TodoTaskControllerTest()
    {
        LongStringFactory = (len) => string.Join("", Fixture.CreateMany<char>(len).ToArray());
    }

    [Fact]
    private async Task CreateTodoFailWhenTitleCraterMoreThan250Characters()
    {
        CreateOrUpdateTodoTaskDto request = Fixture.Build<CreateOrUpdateTodoTaskDto>()
            .With(x => x.Title, LongStringFactory(251))
          .Create();