Strengthen tests by checking the correct error code is returned

Same as above.