0

Is it possible to mock some properties on an object using Jasmine .spyOn()?

Basically, imagine I have a Page object that has title, deliveryTime and status that I want to mock it such that I just have to set the title property title = 'test' ; the other two properties can have any value (of their correct type) as are not relevant for my testing scenario.

Some test fake code would be:

spyOnProperty(page, "title").and.returnValue(test);

This would be equivalent to C# / Moq as:

mock.Setup(foo => foo.Name).Returns("bar");
Denisa.B
  • 11
  • 1
  • 4
  • If the other two properties are irrelevant for testing, why not just set them to any value and only set the `title` value as required? Why do you need to mock it, are you interested in checking when/if the properties are accessed? From the question it seems you are only interested in setting the values of the properties. – Fabian Küng Jan 18 '19 at 06:58
  • This is what I'm doing at the moment, however objects that I'm testing also have more than 3 properties and the unit test setup for each test can grow significantly - to the point where it is very hard to identify the act and assert from the test setup. This in turn also makes it hard to identify the values that have been defined for the test purpose. – Denisa.B Jan 19 '19 at 11:24
  • You can spy on properties as seen in [this](https://stackoverflow.com/a/43984266/4606040) post, but your object needs to implement the `getter` for the spied upon property, otherwise it won't work. – Fabian Küng Jan 20 '19 at 09:31

0 Answers0