using jasmine / spyOn to mock an object with only particular properties defined

1 025 shikime
Kalo te mesazhi i parë i palexuar

Denisa Bokar

e palexuar
25 jan 2019, 11:40:23 e paradites25.1.19
për Jasmine
Is it possible to mock an object using Jasmine .spyOn(), with only a subset of properties actually defined?

Basically, imagine I have a Page object that has deliveryTime, status, size properties on it. In my unit test, the values for status, size are not relevant - only the value in deliveryTime property is.  Some test fake code would be: spyOnProperty(page, "deliveryTime").and.returnValue(123); 

This would be equivalent to C# / Moq as: mock.Setup(foo => foo.Name).Returns("bar"); (more on C# Moq here https://github.com/Moq/moq4/wiki/Quickstart#properties)

Is there a way to achieve this? 

Note: My application is using Angular (7) with GraphQL with an angular GraphQL typescript generator. 

Gregg Van Hove

e palexuar
29 jan 2019, 8:19:16 e pasdites29.1.19
për jasmi...@googlegroups.com
Jasmine's `spyOn` and `spyOnProperty` functions are meant for partial mocking on an existing object basically how you're describing. If you're looking for a way to provide a bare object with some functions that are spies, you should just need to make a new object with a property whose value is a spy created with `jasmine.createSpy`. I would probably not use a spied property in this case where a simple value will do.

Hope this helps. Thanks for using Jasmine!

- Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.
Përgjigju të gjithëve
Përgjigjju autorit
Transfero
0 mesazhe të reja