Controller spec: testing that scope is set

On Mon, Apr 20, 2009 at 1:26 PM, Michael S. [email protected]
wrote:

On Monday 20 April 2009, Pat M. wrote:

In a functional test, create some records that will be in the scope
and some that will be out of the scope, hit the page and make sure
you only see the ones that you want. I would either do this with
cucumber, or write a controller spec and verify that only certain
records show in the the assigns var. Either way you’re going to have
to hit the db because obviously the scope affects the db queries.

Yes, and that feels a bit like cheating.

What feels like cheating, and why?

Look at the BDD process again.

  1. Write failing acceptance test
  2. Drill down, write failing unit tests
  3. Make unit tests pass. Go to 2 until
  4. Acceptance test passes, go to the pub

In this case, you would

  1. Write failing acceptance test
  2. Realize that the code you’re writing is not unit testable
  3. Unit test other parts of code
  4. Acceptance test passes, go to the pub

This is why you’d typically like to keep that stuff in the model.

I agree on the typically, but disagree specifically.

Agreed, which is why I gave a solution instead of just saying to put
it in the model.

Pat

On Sunday 19 April 2009, Michael S. wrote:

In a Rails controller I set the scope on a model class in an around
filter. I have defined expectations on the model classes, and
ideally, I would add a further expectation for the scope. Is this
already possible in some way? How would I go about adding support a
scope expectation?

I’ve found a way to duck punch my way into Spec::Mocks::Proxy and
Spec::Mocks::MessageExpectation to check in #matches that a scope
expectation is met. I’m stuck now, because I don’t see how to
appropriately report a violated expectation. So far, the invasive
changes to both classes are completely general; the Rails-specifics are
patched in from outside. That’s how it should be, of course. Can anyone
suggest how to achieve this for reporting?

I’ve attached the changes I’ve made so far.

Michael