ruby - Writing a negative test by using exception handling -


let have login method that's working swimmingly. it's used in quite number of places , saves plenty of lines of retyped code. intents , purposes, not make sense make method shorter.

is proper write negative test using exception handling? example, if wanted ensure disabled user not able log system, awkward write like:

begin   login(username, password)   fail rescue exception   page.should have_text('sorry, account disabled!') end 

if login succeeds, test should fail. , if reach exception, find our error message , test passes.

i'm wondering if "clever" , may cause confusion. what's best way of handling negative test case here?

you testing expected behavior there no rescuing within specs. in case, want disable account in before caluse , ensure exception raised when attempt perform operation (login, etc.).

testing generic exception bad idea - want specific expected exception different issue cause specs pass.

the code like

expect { login(username, password) }.to raise_error loginexception 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -