ruby - Conditional tests in Rspec? -
is there way group tests conditionally rspec? mean, there way "if variable value, run set of tests. if variable other variable, run other set of tests"?
basic example of needed (doesn't work, obviously, should show want). assume user tested defined elsewhere , current user being tested @user. although may have better alternatives that, that's fine.
before login_as_user(@user) #this logs them in , brings them homepage tested page.visit("/homepage") end describe "check user homepage" subject {page} {should have_content("welcome, #{@user.name}!")} if(@user.role=="admin") {should have_link("list users"} end end
keep in mind have no control on user being tested - cannot create users on fly, example, , no given user guaranteed exist, , don't know offhand combination of roles given user have. need way "run test if these conditions met", rather way create situations every test can run.
you can use let
(or possibly let!
) define user being logged-in should be. (obviously replace @regular_user
& @admin_user
appropriate factory/fixture/etc.)
before login_as_user(user) page.visit "/homepage" end let(:user) { @regular_user } describe "check user homepage" subject { page } { should have_content "welcome, #{@user.name}!" } context "when administrator" let(:user) { @admin_user } { should have_link "list users" } end end
Comments
Post a Comment