This is my rspec integration test for user, it always breaks when my teammate change something, how do i improve this?
describe "Users" do
before(:each) do
populate_role
end
describe "GET /users/sign_in" do
#it "should redirect to redirect to user sign in page for user that is not sign in" do
# get root_path
# response.should redirect_to(new_user_session_url)
#end
it "should sign in the admin with correct user id and password" do
login_user(@admin_role)
page.should have_content("Sign out")
end
end
describe "DELETE /users/sign_out" do
it "shoud sign out the admin" do
login_user(@admin_role)
page.should have_content("Sign out")
visit root_url
click_link "Sign out"
page.should have_content("Signed out successfully.")
end
end
describe "GET /users/new" do
it "should allow admin to create a new owner with only user_id and generate the password" do
owner = FactoryGirl.build(:user, role: @owner_role, unit: FactoryGirl.create(:unit))
admin = login_user(@admin_role)
visit new_user_path
fill_in_admin_user_creation_form_for(owner)
click_button("Add")
page!
#page.should have_content("User was successfully created.")
should_have_content_for_admin_user_creation(owner)
end
it "should allow owner to create resident" do
owner = login_user(@owner_role, FactoryGirl.create(:unit))
resident = FactoryGirl.build(:user, role: @resident_role)
visit new_user_path
fill_in_owner_user_creation_form_for(resident)
click_button("Add")
should_have_content_for_owner_user_creation(resident)
end
it "should not allow owner to create admin and change unit" do
login_user(@owner_role, FactoryGirl.create(:unit))
visit new_user_path
page.should have_no_selector('select#user_role_id')
page.should have_no_selector('select#user_unit_id')
end
end
# print user info
describe "GET /user/:id/print" do
it "should allow admin to print the user information include self generated password after admin created the owner" do
login_user(@admin_role)
owner = FactoryGirl.build( :user, role: @owner_role, unit: FactoryGirl.create(:unit) )
visit new_user_path
fill_in_admin_user_creation_form_for(owner)
click_button("Add")
find("#user_password").should have_content(owner.user_id)
end
it "should print the user information include password before user activate it" do
login_user(@admin_role)
owner = FactoryGirl.create( :user, :without_password, role: @owner_role, unit: FactoryGirl.create(:unit))
visit user_print_path(owner)
find("#user_password").should have_content(owner.user_id)
end
it "should print the user information of resident that is under the owner" do
owner = login_user(@owner_role, FactoryGirl.create(:unit))
resident = FactoryGirl.create( :user, :without_password, role: @resident_role, unit: owner.unit)
visit user_print_path(resident)
current_path.should == user_print_path(resident)
end
it "should not print the user information of resident that is not under the owner" do
owner = login_user(@owner_role, FactoryGirl.create(:unit))
resident = FactoryGirl.create( :user, :without_password, role: @resident_role, unit: FactoryGirl.create(:unit))
visit user_print_path(resident)
current_path.should == root_path
end
it "should print the user information of owner that is not activated" do
admin = login_user(@admin_role)
owner = FactoryGirl.create( :user, :without_password, role: @owner_role, unit: FactoryGirl.create(:unit),active: false)
visit user_print_path(owner)
current_path.should == user_print_path(owner)
end
it "should not print the user information of owner that is activated" do
admin = login_user(@admin_role)
owner = FactoryGirl.create( :user, :without_password, role: @owner_role, unit: FactoryGirl.create(:unit),active: true)
visit user_print_path(owner)
current_path.should == root_path
end
end
def fill_in_admin_user_creation_form_for(user)
fill_in("user[user_id]", :with => user[:user_id])
select(user.role.name, :from => "user[role_id]")
select(user.unit.to_string, :from => "user[unit_id]")
select(user[:title], :from => "user_title")
fill_in("user[name]", :with => user[:name])
fill_in("user[dob]", :with => user[:dob])
fill_in("user[nric]", :with => user[:nric])
fill_in("user[phone]", :with => user[:phone])
fill_in("user[email]", :with => user[:email])
end
def fill_in_owner_user_creation_form_for(user)
fill_in('user[user_id]', :with => user[:user_id])
select(user[:title], :from => "user[title]")
fill_in("user[name]", :with => user[:name])
fill_in("user[dob]", :with => user[:dob])
fill_in("user[nric]", :with => user[:nric])
fill_in("user[phone]", :with => user[:phone])
fill_in("user[email]", :with => user[:email])
end
def should_have_content_for_admin_user_creation(user)
page.should have_content(user[:user_id])
page.should have_content(user.unit.to_string)
page.should have_content(user[:title])
page.should have_content(user[:name])
page.should have_content(user[:dob])
page.should have_content(user[:nric])
page.should have_content(user[:phone])
page.should have_content(user[:email])
end
def should_have_content_for_owner_user_creation(user)
page.should have_content(user[:user_id])
page.should have_content(user.unit.to_string)
page.should have_content(user[:title])
page.should have_content(user[:name])
page.should have_content(user[:dob])
page.should have_content(user[:nric])
page.should have_content(user[:phone])
page.should have_content(user[:email])
end
end