Topic: [rSpec] Testing Database Interactions

Hello again, another rSpec question which I hope you can help with.

I have the following method in my User model

def self.find_and_activate!(activation_key)
  raise ArgumentError if activation_key.nil?
  user = find_by_activation_key(activation_key)
  raise ActivationCodeNotFound if !user
  raise if user.activated?

The private activate! method just updates activated_at and sets an instance variable.

How do I go about testing this using BDD and rSpec? I need something already in the database, but I don't think I can do a fixture because the activation key is randomly generated. Can I create a record in the example and then test that? Does that adhere to BDD best practices and how do I go about it?

Any help on this would be greatly appreciated!



Re: [rSpec] Testing Database Interactions

Create some mocks that describe what the proper state change is. I don't understand your application, but here's a skeleton of what I'd start with:

@my_mock = mock("whatever_mock")
@user = mock_model(User)

Put this right after the describe and it will only affect the example that tests this stuff. Does this help?