chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hello,

I'm trying to mock and test the following code:

PageSmartPartInfo info = new PageSmartPartInfo(...)
MainView.SmartPartInfo = info;


So I'm mocking MainView object and I want to test the info value.
How can I get the value that was set into SmartPartInfo of my mocked object? is there any other way of testing that value?

Thanks,

Adam
asked by adamoren (1.1k points)

3 Answers

0 votes
Hi
You need to set expectation on MainView.SmartPartInfo
If you are using reflective mocks it will be something like this:
//setting expectations
Mock mockPageSmartPartInfo = MockManager.Mock<MainView>();
mockPageSmartPartInfo.ExpectGet("SmartPartInfo", new PageSmartPartInfo());

//calling our code
PageSmartPartInfo smartPartInfo = MainView.SmartPartInfo;

//Verify that MainView.SmartPartInfo called
MockManager.Verify();


The same but using natural mocks:
//setting expectations
using (RecordExpectations record = new RecordExpectations())
{
    record.ExpectAndReturn(MainView.SmartPartInfo, new PageSmartPartInfo());
}

//calling our code
PageSmartPartInfo smartPartInfo = MainView.SmartPartInfo;

//Verify that MainView.SmartPartInfo called
MockManager.Verify();


Note that I assumed that:
1. MainView.SmartPartInfo is static property
2. You want to mock the property.

Please correct me if I'm wrong.
answered by ohad (35.4k points)
0 votes
Hi Ohad,

ma hamazav? :)

Actually, my intensions where to test the method containing this code and not the property. so what I wanted to test was that the SmartPartInfo property is being set correctly. if I'll mock that I will be mocking the very thing that I want to test.
In other words, I want to test the value being set to this property to be an expected value.

Can it be done?

Thanks again,

Adam
answered by adamoren (1.1k points)
0 votes
Hi Adam

Maybe what you are looking for is this?
Mock mock = MockManager.Mock<MainView>();
mock.ExpectUnmockedSet("SmartPartInfo");


This way you can verify that the set property was called plus the value
of MainView.SmartPartInfo is 'real' so you can use Assert to check its value.

Hope I got your meaning this time :)
answered by ohad (35.4k points)
...