Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide block syntax for sequences #61

Closed
floehopper opened this issue Feb 1, 2012 · 3 comments · Fixed by #631
Closed

Provide block syntax for sequences #61

floehopper opened this issue Feb 1, 2012 · 3 comments · Fixed by #631

Comments

@floehopper
Copy link
Member

It should be possible to provide the following syntax for a simple sequence i.e. one that is not interleaved with another :-

in_sequence(:task_order) do
  task_one.should_receive(:execute)
  task_two.should_receive(:execute)
end

Any expectations within the block would be assigned to the sequence specified in the block.

See https://gist.github.com/1704783 for some relation discussions.

@viking
Copy link
Contributor

viking commented Mar 20, 2014

Any update with this? I've taken to writing a little helper in my tests so that I can use <<:

class SequenceHelper
  def initialize(name)
    @seq = sequence(name)
  end

  def <<(expectation)
    expectation.in_sequence(@seq)
  end
end

@viking
Copy link
Contributor

viking commented Apr 17, 2014

I noticed the sequence experiment branch. I came up with almost the same implementation independently for sequence blocks.

Some other ideas:

  1. use set_trace_func (or TracePoint) to add constraints to expectations
  2. change execution context of block so that current_sequence has a different value (Mock#current_sequence could always return nil)
  3. change execute context of block so that local variables are actually wrappers that add constraints to expectations

For option 3, a trick is needed. As far as I can tell, any local variables in the block binding supersede everything else, so we'd have to get the source of the block (using Proc#source_location) and eval the code to make a new block, and then use method_missing to wrap the objects.

Any thoughts?

floehopper added a commit that referenced this issue Nov 18, 2023
All expectations defined within the block are constrained by the
sequence:

    egg = mock('egg')
    sequence('breakfast') do
      egg.expects(:crack)
      egg.expects(:fry)
      egg.expects(:eat)
    end

Closes #61 (only 11 years late!)
floehopper added a commit that referenced this issue Nov 18, 2023
All expectations defined within the block are constrained by the
sequence.

For example, the following requires that Egg#crack must be called 1st,
Egg#fry must be called 2nd, and Egg#eat must be called 3rd:

    egg = mock('egg')
    sequence('breakfast') do
      egg.expects(:crack)
      egg.expects(:fry)
      egg.expects(:eat)
    end

Closes #61 (only 11 years late!)
floehopper added a commit that referenced this issue Nov 18, 2023
All expectations defined within the block are constrained by the
sequence.

For example, the following requires that Egg#crack must be called 1st,
Egg#fry must be called 2nd, and Egg#eat must be called 3rd:

    egg = mock('egg')
    sequence('breakfast') do
      egg.expects(:crack)
      egg.expects(:fry)
      egg.expects(:eat)
    end

Closes #61 (only 11 years late!)
floehopper added a commit that referenced this issue Nov 18, 2023
All expectations defined within the block are constrained by the
sequence.

For example, the following requires that Egg#crack must be called 1st,
Egg#fry must be called 2nd, and Egg#eat must be called 3rd:

    egg = mock('egg')
    sequence('breakfast') do
      egg.expects(:crack)
      egg.expects(:fry)
      egg.expects(:eat)
    end

Closes #61 (only 11 years late!)
floehopper added a commit that referenced this issue Nov 18, 2023
All expectations defined within the block are constrained by the
sequence.

For example, the following requires that Egg#crack must be called 1st,
Egg#fry must be called 2nd, and Egg#eat must be called 3rd:

    egg = mock('egg')
    sequence('breakfast') do
      egg.expects(:crack)
      egg.expects(:fry)
      egg.expects(:eat)
    end

Closes #61 (only 11 years late!)
@floehopper
Copy link
Member Author

Released in v2.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants