ruby on rails - What tests should I write to test user validations for an admin-only feature? -
i created rspec spec test if post #create action works properly:
describe "post #create" "creates career" expect { post "/careers/", career: attributes_for(:career) }.to change(career, :count).by 1 end end the above code works correctly. issue happens when create test allow users roles of "admin". need create new user, log them in, , run above test? need future tests have restriction based on user's role?
is there way type of testing? 1) test if create method works, , 2) allow users "admin" role access #new , post #create methods?
when feature developed you'll want have following tests:
- one happy-path test in admin creates career
- one in non-admin tries create career prevented doing so
- possibly in not-logged-in user tries create career prevented doing (whether want depends on whether have write different code handle non-logged-in , logged-in non-admin users), and
- possibly other tests of different scenarios in admin creates career.
this idea of having 1 complete, happy-path test 1 of fundamental patterns in testing, i'm not aware has name, other being implied term "happy path".
it looks you're doing tdd. great! above list of tests, next test write 1 non-logged-in user prevented creating career. make both tests pass @ same time you'll need change first test log in admin. , if need more tests of creating career (bullet 4), yes, you'll need log in admin in too.
side notes:
unless have it, i'd write happy-path spec not controller spec feature spec (an acceptance test), specify important parts of ui , integration-test entire stack. failed-authentication specs might work controller specs, although might decide need acceptance-test ui when user doesn't have permissions @ least 1 of scenarios.
i don't
expect {}.to changesyntax. prevents making other expectations on result of posting. in example want expect http response status 200 (response.should be_success). said, though, first spec feature spec, not controller spec.
Comments
Post a Comment