ruby on rails - rspec test for destroy if user doesn't exist -
I am using rspec + FactoryGirl to test my app.
I have the next test
context "user can not be deleted" (from First of all) Do @User = FactoryGrilled Create User Delete: Delete, {id: 1}, format :: Jessan is expected to "render the error Jesns if not destroy" the user ends, Ughr_propusn = Jesoan (Pratikriyakcom, Prtik_any: true) (User_Decutions [: Errors] [: Messages]). To include "The user can not be destroyed" end it should answer from the end of {422} When I run the test, I get the error ' User not found with id '= 2 . If I change the ID {id: @ user.id} then the exam is not passed and I get further errors.
is the expected response to a 422: NilClass and
for the undefined method `[] ' , But 204 I test code User.estroy json: {success: {message = & gt;}
< Code> def user = User.find (params [: id]); "User deleted"}}, Position: 204 Other render Jason: {errors: {: message = & gt; "User can not be deleted"}}, position: 422 and end So, I have two questions:
- How do I fix it Can i do
- Need to check whether the user has been deleted or not? (If user.destroy ...)
If the second question is false, then I think my life will be easy.
This part of your test:
@user = factorygirl Create: Delete User: Delete, {id: 1}, Format:: Json Agrees that code to 1 will be specified. It is not a safe assumption that you can change this line to:
delete: deleted, {id: @ user.id}, format :: json After that change, you say that the test fails:
expected answer to be a 422, but 204 This indicates that the user is is destroyed.
The easiest solution is to change your controller:
def user = User.find (params [: id]) render json: {errors: {message = & Gt; In the real world, there is a situation where the user can be destroyed by , in which case you will need some logic in your controller.
Comments
Post a Comment