Repeat Expectations in Expectations

View: New views
8 Messages — Rating Filter:   Alert me  

Repeat Expectations in Expectations

by ecor6633 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,
I'm new to Jmock and what i'm looking for, is a way to repeat a list of expectaions (like a sequence) an unknown number of time inside Expectations.

See my comments in that code block :

context.checking(new Expectations() {
{
        one(mock).init();
       
        ...
       
        final Sequence element = context.sequence("element");
  for (int i = 0; i < 416; i++) {
    one(mock).beginDataType(StructureDataType.ELEMENT); inSequence(element);
    one(mock).addProperties(StructurePropertieName.ELEMENT_NUMBER, with(any(int.class)) ); inSequence(element);
    one(mock).addProperties(StructurePropertieName.ELEMENT_TYPE, with(any(int.class)) ); inSequence(element);
    one(mock).addProperties(StructurePropertieName.ELEMENT_MATERIAL, with(any(int.class)) );  inSequence(element);
    one(mock).addProperties(StructurePropertieName.ELEMENT_GEOMETRY, with(any(int.class)) );
    one(mock).addProperties(StructurePropertieName.ELEMENT_INTEGRATION_POINT, with(any(int.class)) );  inSequence(element);
    one(mock).addProperties(StructurePropertieName.ELEMENT_DESFIN_TYPE, with(any(int.class)) ); inSequence(element);
    one(mock).addProperties(StructurePropertieName.ELEMENT_DEGREE, with(any(int.class)) ); inSequence(element);
   
//  Here inside my element i need to repeat a sequence i would call trace which looks like
//  The problem is that i can't determine the number of time
//  final Sequence trace = context.sequence("trace");
//  one(mock).beginDataType(StructureDataType.TRACE); inSequence(trace);
//  one(mock).addProperties(StructurePropertieName.TRACE_NODE, with(any(int.class)) ); inSequence(trace);
//  one(mock).addProperties(StructurePropertieName.TRACE_PEN, with(any(boolean.class)) ); inSequence(trace);
//  one(mock).endDataType(StructureDataType.TRACE); inSequence(trace);
  }
 
  one(mock).end();
}

Is there a way to do like atLeast(1).of(trace) where trace is a sequence I defined ?

Re: Re[jmock-user] peat Expectations in Expectations

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you give us more background to what you're testing? This looks too  
big for a unit test.

The test suggests that the code is missing some intermediate  
structure, but it's hard to tell

S.

On 13 May 2008, at 13:58, ecor6633 wrote:

>
> Hello,
> I'm new to Jmock and what i'm looking for, is a way to repeat a list  
> of
> expectaions (like a sequence) an unknown number of time inside  
> Expectations.
>
> See my comments in that code block :
>
> context.checking(new Expectations() {
> {
> one(mock).init();
>
> ...
>
> final Sequence element = context.sequence("element");
>  for (int i = 0; i < 416; i++) {
>    one(mock).beginDataType(StructureDataType.ELEMENT);  
> inSequence(element);
>    one(mock).addProperties(StructurePropertieName.ELEMENT_NUMBER,
> with(any(int.class)) ); inSequence(element);
>    one(mock).addProperties(StructurePropertieName.ELEMENT_TYPE,
> with(any(int.class)) ); inSequence(element);
>    one(mock).addProperties(StructurePropertieName.ELEMENT_MATERIAL,
> with(any(int.class)) );  inSequence(element);
>    one(mock).addProperties(StructurePropertieName.ELEMENT_GEOMETRY,
> with(any(int.class)) );
>
> one
> (mock).addProperties(StructurePropertieName.ELEMENT_INTEGRATION_POINT,
> with(any(int.class)) );  inSequence(element);
>    one(mock).addProperties(StructurePropertieName.ELEMENT_DESFIN_TYPE,
> with(any(int.class)) ); inSequence(element);
>    one(mock).addProperties(StructurePropertieName.ELEMENT_DEGREE,
> with(any(int.class)) ); inSequence(element);
>
> //  Here inside my element i need to repeat a sequence i would call  
> trace
> which looks like
> //  The problem is that i can't determine the number of time
> //  final Sequence trace = context.sequence("trace");
> //  one(mock).beginDataType(StructureDataType.TRACE);  
> inSequence(trace);
> //  one(mock).addProperties(StructurePropertieName.TRACE_NODE,
> with(any(int.class)) ); inSequence(trace);
> //  one(mock).addProperties(StructurePropertieName.TRACE_PEN,
> with(any(boolean.class)) ); inSequence(trace);
> //  one(mock).endDataType(StructureDataType.TRACE); inSequence(trace);
>  }
>
>  one(mock).end();
> }
>
> Is there a way to do like atLeast(1).of(trace) where trace is a  
> sequence I
> defined ?
> --
> View this message in context: http://www.nabble.com/Repeat-Expectations-in-Expectations-tp17207755p17207755.html
> Sent from the jMock - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re[jmock-user] peat Expectations in Expectations

by ecor6633 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a file structure definition to read, so, many files can match that structure.
I've written a reader that looks like a sax parser, it generates events (call methods). As i know the structure of the file, i want those events in a certain order.
I could read any file and get the same order so what i would like to do is create a mock test that only verify that my reader calls my methods in the right order.
But as you seen, for a certain part i have to accept a certain sequence but any number of time, at least once.

I hope it makes it a bit clearer and if you think of an other way of testing my reader, you're welcome.

Eric.

Steve Freeman-2 wrote:
Can you give us more background to what you're testing? This looks too  
big for a unit test.

The test suggests that the code is missing some intermediate  
structure, but it's hard to tell

S.

Re: Re: Re[jmock-user] peat Expectations in Expectations

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

So you're running this over a large file? At the very least, I'd cut  
the test down to a small number of representative values. Tests should  
be specific, rather than trying to be generic.

Would it make more sense to have proper structures, with a callback  
for each, rather than just a sequence of properties? It would remove  
the need for some of the start/end calls.

Finally, is addProperties() the right name for the method? Is that  
what it's actually doing? If it's a callback, it ought to be something  
like onProperty(), or whatever convention you prefer.

S.


On 14 May 2008, at 09:17, ecor6633 wrote:

>
> I have a file structure definition to read, so, many files can match  
> that
> structure.
> I've written a reader that looks like a sax parser, it generates  
> events
> (call methods). As i know the structure of the file, i want those  
> events in
> a certain order.
> I could read any file and get the same order so what i would like to  
> do is
> create a mock test that only verify that my reader calls my methods  
> in the
> right order.
> But as you seen, for a certain part i have to accept a certain  
> sequence but
> any number of time, at least once.
>
> I hope it makes it a bit clearer and if you think of an other way of  
> testing
> my reader, you're welcome.
>
> Eric.
>
>
> Steve Freeman-2 wrote:
>>
>> Can you give us more background to what you're testing? This looks  
>> too
>> big for a unit test.
>>
>> The test suggests that the code is missing some intermediate
>> structure, but it's hard to tell
>>
>> S.
>>
>
> --
> View this message in context: http://www.nabble.com/Repeat-Expectations-in-Expectations-tp17207755p17225949.html
> Sent from the jMock - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: Re[jmock-user] peat Expectations in Expectations

by ecor6633 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Steve Freeman-2 wrote:
So you're running this over a large file? At the very least, I'd cut  
the test down to a small number of representative values. Tests should  
be specific, rather than trying to be generic.
Yes i have a large file but now i've a  test on a few lines i wrote myself so that i can make a for loop knowing the number of times methods call should occur.
Maybe that's enough and i shouldn't try to make this test pass with any file unless i want to test the file and not reader :-P

Steve Freeman-2 wrote:
Would it make more sense to have proper structures, with a callback  
for each, rather than just a sequence of properties? It would remove  
the need for some of the start/end calls.
I have to use a builder that makes the abstraction for other reader that could read other files in an ohter way. That's just a way to detach the reader from any structure which are builded up by the builder.

Steve Freeman-2 wrote:
Finally, is addProperties() the right name for the method? Is that  
what it's actually doing? If it's a callback, it ought to be something  
like onProperty(), or whatever convention you prefer.

S.
It really sets a property on the current object it's currently building but you're right as it's a kind of event we could rename it.

Thanks.

So my problem is "solved" but i still wonder if it's be possible to say in a jmock atLeast(1).of a sequence ...

Re: Re: Re: Re[jmock-user] peat Expectations in Expectations

by Nat Pryce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/5/14 ecor6633 <ericcornely@...>:
> I wonder if it's be possible to say in a
> jmock atLeast(1).of a sequence ...

No. Sequences do not repeat.  For that you need to use a state machine
with a transition from the final state back to the first state.

--Nat.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: Re: Re[jmock-user] peat Expectations in Expectations

by Dale King-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It does not seem like that unreasonable of a request to specify repeat
counts of a sequence. It may however be a major tear up to implement.

On Thu, May 15, 2008 at 3:07 AM, Nat Pryce <nat.pryce@...> wrote:

> 2008/5/14 ecor6633 <ericcornely@...>:
>> I wonder if it's be possible to say in a
>> jmock atLeast(1).of a sequence ...
>
> No. Sequences do not repeat.  For that you need to use a state machine
> with a transition from the final state back to the first state.
>
> --Nat.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
Dale King

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: Re: Re[jmock-user] peat Expectations in Expectations

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

it probably wouldn't be too hard, but I think it's missing the point.

First, as Nat pointed out, for many of the situations, you're probably  
better off using states.

Second, you can always write a helper method to create a loop.

Third, you really shouldn't this kind of behaviour in a unit test.  
There should be almost no cases that require this complexity of  
expectation, it's almost certainly a sign that the code needs breaking  
up -- or that there's too much test data.

In the original poster's problem, the real issue was unit testing  
against an enormous file, when one or two cases would have made the  
point.

S.

On 15 May 2008, at 15:07, Dale King wrote:

> It does not seem like that unreasonable of a request to specify repeat
> counts of a sequence. It may however be a major tear up to implement.
>
> On Thu, May 15, 2008 at 3:07 AM, Nat Pryce <nat.pryce@...>  
> wrote:
>> 2008/5/14 ecor6633 <ericcornely@...>:
>>> I wonder if it's be possible to say in a
>>> jmock atLeast(1).of a sequence ...
>>
>> No. Sequences do not repeat.  For that you need to use a state  
>> machine
>> with a transition from the final state back to the first state.
>>

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price