Mockolate - Calling the same method on a mock with the same arguments but return different results

StackOverflow https://stackoverflow.com/questions/11312108

Вопрос

I'am using Mockolate and flex unit 4 to write mocks for my tests in AS3. I want to call the same method on a mock with the same arguments multiple times and return a different result each time. A basic example is:

            [Rule] 
            public var mocks:MockolateRule = new MockolateRule(); 

            [Mock(type="strict")] 
            public var list:IList; 

            [Test] 
            public function test():void{ 

                    var seq:Sequence = sequence(); 
                    expecting(function ():void { 
                            expect(list.getItemAt(0)).returns("Item1").thrice().inSequence(seq); 
                            expect(list.getItemAt(0)).returns("Item2").inSequence(seq); 
                            expect(list.getItemAt(0)).returns("Item3").inSequence(seq); 
                    }); 

                    trace(list.getItemAt(0)); 
                    trace(list.getItemAt(0)); 
                    trace(list.getItemAt(0)); 

                    verify(list); 
            } 

This results in:

Item1 
Item2 
Item2 

And and error:

1 unmet Exception

What I want is

Item1 
Item2 
Item2 

and no exceptions, i.e. test passes.

I've tried a few alternatives such as:

                    expecting(function ():void { 
                            expect(list.getItemAt(0)).returns("Item1").inSequence(seq); 
                            expect(list.getItemAt(0)).returns("Item2").inSequence(seq); 
                            expect(list.getItemAt(0)).returns("Item3").inSequence(seq); 
                    }); 

and others to no avail. Surely I'm missing something simple here. This seems like something that should be easy.

Thanks guys,

Theo.

Это было полезно?

Решение

This bugged the hell out of me, until I found out it is really very simple: Just use all the expected return values as parameters to the first returns() statement.

 expect(list.getItemAt(0)).returns("Item1", "Item2", "Item3", "Item4").inSequence(seq); 

The mock will return the last of these parameters for each subsequent call.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top