简体   繁体   中英

Simulate read-only database with Rspec / Rails

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).

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM