I am trying to test (with Jasmine) validation of a description field (which is a knockout observable) with max length of 1000 characters;
What I usually do is expect(model.description().length).toBe(1000);
to prove that my string is of the desired length.
Is there any way to mock it somehow without the need to actually write a string of 1000 chars long?
Asked
Active
Viewed 5,100 times
1

koninos
- 4,969
- 5
- 28
- 47
-
7you could use `Array(1001).join("a")` – Hacketo Dec 22 '15 at 15:49
-
Thank you, if no other way, then I will use it :) – koninos Dec 22 '15 at 15:51
-
What is the purpose of checking the length of a string that is generated just to pass the test? – GOTO 0 Dec 22 '15 at 15:52
-
You can monkey patch `model.description` and put a mock of an observable in there that has a settable `length` property. But this feels like an XY-problem to me :-) – Jeroen Dec 22 '15 at 15:53
-
@GOTO0 There isn't, I just used it here to explain what I mean. – koninos Dec 22 '15 at 15:54
-
4In that case, something like `".".repeat(1000)` will do it, or even just `{ length: 1000 }` if you're only interested in the length property. – GOTO 0 Dec 22 '15 at 15:59
1 Answers
1
If you are intent on mocking the object and just want to use the length
property and nothing else then as mentioned by @GOTO-0, you can just create an object with that property set like this: { length: 1000 }
.
However, it seems easier to just create a string that is that length. This could help as if some of the code changes to require an actual string elsewhere, the test will still be valid rather than you having to start mocking other methods and properties of a string. See this question for discussions on how to do that best: Create a string of variable length, filled with a repeated character

Community
- 1
- 1

TomDoesCode
- 3,580
- 2
- 18
- 34