1

I'm currently running the unit tests of my RoR application with Rspec. One of my controllers should return a HTTP status 500 when it can not write a new entity to the database. I wish to test this behaviour and would like temporarily to mark my database is read-only.

Does anyone know if it is possible to do this?

I tried the technique mentioned in "How do I prepare test database(s) for Rails rspec tests without running rake spec?", but this approach does not work for me (since the controller reads from the database before writing to it).

Community
  • 1
  • 1
Arno Moonen
  • 1,134
  • 2
  • 10
  • 28

1 Answers1

2

I think you can just mock out corresponding Model requests, if you are testing the controller. So, what exception should Model.create throw? Try something like this:

Model.stub(:create).and_raise(ActiveRecord::ReadOnlyRecord)
roman-roman
  • 2,746
  • 19
  • 27
  • I guess this should work when I'd be using the `Model.create` method, but I'm using `Model.new` in combination with `instance.save`. Not quite sure how I would use `stub` in this case. – Arno Moonen Apr 22 '14 at 13:02
  • Maybe, stub `save` then? – roman-roman Apr 22 '14 at 13:05
  • I think, if you want to *unit* test, then go with stubbing. If you would go with feature tests - than manipulate the DB. – roman-roman Apr 22 '14 at 13:05
  • I tried stubbing `save` but that does not seem to work. I had another look at my code and it turns out I can use create directly, instead of using the two methods mentioned before. I've implemented this and used your example. Seems to work just fine. Thanks! :) – Arno Moonen Apr 22 '14 at 13:08