Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is my rspec integration test for user , it always break 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
share|improve this question
add comment

1 Answer

Your specs look OK (I have a few comments, but none that would address your main question of why your specs break when your teammates push code). I suspect the problem lies not with your code, but with the team - sounds like you're trying to test drive, and they're not on-board?

If everyone followed the process, that shouldn't happen. Specifically, if:

  • Everyone Test Drove and followed the principle of not writing code unless there was a failing spec: First write a spec, watch it fail, then write the minimum amount of code to fix the failure, repeat.
  • Everyone ran all the specs before pushing code to make sure they didn't break anything

Then this wouldn't happen. You should evangelize TDD and get your teammates to appreciate the benefits!

My comments, FWIW:

  • You're mixing contexts a little bit with your descriptions: "GET /users/sign_in" and "DELETE /users/sign_out" type descriptions belong in Controller specs and not Request specs. (The request spec simulates an actual user, who has no knowledge of HTTP methods, etc. I would write it more like so:

    context "admin logging in" do
      describe "when an admin signs in successfully" do
        it "should indicate that he is signed in by displaying the sign-out button" do
          ....
        end
        end
        describe "when and admin signs out" do 
          it "should show a success message" do
            ....
          end
      end
    end
    
  • FactoryGirl has an option to allow using just create(:unit) instead of FactoryGirl.create(:unit). Makes things a bit more readable.

  • Delete commented out code - it clutters things up, you can always retrieve it from git.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.