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
I have the following code:
    public  class MyWrap
    {
        public MyBase baseAttach
        { get; set; }
 //snip all the fake properties
    }
    public  class MyBase
    {
        
    }


Here's the test code
        [Test, Isolated]
        public void BaseTop()
        {
            MyWrap wrap = Isolate.Fake.Instance<MyWrap>(Members.ReturnRecursiveFakes);
            Isolate.WhenCalled(()=>wrap.baseAttach).CallOriginal();
            wrap.baseAttach=new MyBase();
            Assert.IsNotNull(wrap.baseAttach);
        }


As you can see, I want to make sure that all the other properties inside wrap are fake, only the baseAttach is real, so I want to set the baseAttach to a real object, unfortunately the above test fails because even after the setting, wrap.baseAttach is still a null.

Any idea on how to solve this situation? Or is this a bug?
________
Honda CR series
asked by nsoonhui (59.1k points)

7 Answers

0 votes
Hi Soon,

The syntax you used is for the"getter". You need to set the behavior for the setter as well:

Isolate.WhenCalled(()=>wrap.baseAttach = null).CallOriginal(); 


Tne syntax is a bit awkward, but that's what the compiler lets us do.

Thanks,
answered by gilz (14.5k points)
0 votes
You are faking the get part of the property and not the set part:
Add this line to fake both.
Isolate.WhenCalled(() => wrap.baseAttach = null ).CallOriginal();


I understand that this is not intuitive. Here are some ideas that we are thinking about.

1. Fake Properties will act as if they are simple properties
2. WhenCalled(()=>wrap.baseAttach).CallOriginal(); will handle both set and get
3. WhenCalled(()=>wrap.baseAttach).WillReturn(1); will fake sets and always return 1

4. Verify.WasCalled(()=>wrap.baseAttach) for gets
5. Verify.WasSet(()=>wrap.baseAttach).To(5)

What do you think?
answered by eli (5.7k points)
0 votes
Hi Soon,

The syntax you used is for the"getter". You need to set the behavior for the setter as well:

Isolate.WhenCalled(()=>wrap.baseAttach = null).CallOriginal(); 


Tne syntax is a bit awkward, but that's what the compiler lets us do.

Thanks,


Thanks, now I got it.
________
Mercury Montego picture
answered by nsoonhui (59.1k points)
0 votes
You are faking the get part of the property and not the set part:
Add this line to fake both.
Isolate.WhenCalled(() => wrap.baseAttach = null ).CallOriginal();


I understand that this is not intuitive. Here are some ideas that we are thinking about.

1. Fake Properties will act as if they are simple properties
2. WhenCalled(()=>wrap.baseAttach).CallOriginal(); will handle both set and get
3. WhenCalled(()=>wrap.baseAttach).WillReturn(1); will fake sets and always return 1

4. Verify.WasCalled(()=>wrap.baseAttach) for gets
5. Verify.WasSet(()=>wrap.baseAttach).To(5)

What do you think?


I would rather have 2), since when I call WhenCalled(()=>wrap.baseAttach).CallOriginal(); I am actually assuming that both of the get and set are original. So I can do anything to the setters of baseAttach after i call the CallOriginal statement.
________
volcano vaporizer reviews
answered by nsoonhui (59.1k points)
0 votes
Hi,

I am not sure the above suggestion works. Here's my production code:
    public  class MyWrap
    {
        public MyBase baseAttach
        { get; set; }
    }
    public  class MyBase
    {
        public double FirstNumber
        { get; private set; }
        public MyBase()
        {
            FirstNumber = 10; }
    }


And here's the test code
        [Test, Isolated]
        public void BaseTop()
        {
            MyWrap wrap = Isolate.Fake.Instance<MyWrap>(Members.ReturnRecursiveFakes);
            Isolate.WhenCalled(() => wrap.baseAttach = new MyBase()).CallOriginal();
            wrap.baseAttach=new MyBase();
            Assert.AreEqual(10.0, wrap.baseAttach.FirstNumber);
        }


The test fails at the last line because the wrap.baseAttach.FirstNumber is 0.0, not 10.0.

What did I do wrong?
________
List of transmissions
answered by nsoonhui (59.1k points)
0 votes
Hi,

I am not sure the above suggestion works. Here's my production code:
    public  class MyWrap
    {
        public MyBase baseAttach
        { get; set; }
    }
    public  class MyBase
    {
        public double FirstNumber
        { get; private set; }
        public MyBase()
        {
            FirstNumber = 10; }
    }


And here's the test code
        [Test, Isolated]
        public void BaseTop()
        {
            MyWrap wrap = Isolate.Fake.Instance<MyWrap>(Members.ReturnRecursiveFakes);
            Isolate.WhenCalled(() => wrap.baseAttach = new MyBase()).CallOriginal();
            wrap.baseAttach=new MyBase();
            Assert.AreEqual(10.0, wrap.baseAttach.FirstNumber);
        }


The test fails at the last line because the wrap.baseAttach.FirstNumber is 0.0, not 10.0.

What did I do wrong?
________
Daihatsu Leeza history
answered by nsoonhui (59.1k points)
0 votes
Hi Soon

The problem is that you are calling the getter method
wrap.baseAttach.FirstNumber in the Assert line
since you are using Members.ReturnRecursiveFakes on MyWrap class the property returns 0.

The work around is to set the getter with CallOriginal like this:
[Test, Isolated]
public void BaseTop()
{
    MyWrap wrap = Isolate.Fake.Instance<MyWrap>(Members.ReturnRecursiveFakes);
    Isolate.WhenCalled(()=> wrap.baseAttach = null).CallOriginal();
    Isolate.WhenCalled(()=> wrap.baseAttach).CallOriginal();
    wrap.baseAttach = new MyBase();
    Assert.AreEqual(10.0, wrap.baseAttach.FirstNumber);
} 



We are working on a feature that will save you second line and the test
will work as you expected without the workaround.
answered by ohad (35.4k points)
...