2012-07-31 103 views
0

我在做Rails教程,並且一直嚴格遵循它。但是,我的user_pages_spec.rb測試重置了開發數據庫。所以,無論何時我進行測試,我都會丟失測試前存在的所有當前數據。我的猜測是問題出現在第11行,但我不確定。任何幫助是極大的讚賞,感謝:)Rails Rspec測試重置數據庫

這是我的user_pages_spec.rb文件:

require 'spec_helper' 

describe "UserPages" do 

subject { page } 

describe "index" do 
    let(:user) { FactoryGirl.create(:user) } 

    before(:all) { 30.times { FactoryGirl.create(:user) } } 
    after(:all) { User.delete_all } 

    before(:each) do 
     sign_in user 
     visit users_path 
    end 

    it { should have_selector('title', text: 'All users') } 
    it { should have_selector('h1', text: 'All users') } 

    describe "pagination" do 
     it { should have_selector('div.pagination') } 

     it "should list each user" do 
      User.paginate(page: 1).each do |user| 
       page.should have_selector('li', text: user.name) 
      end 
     end 
    end 

    describe "as an admin user" do 
     let(:admin) { FactoryGirl.create(:admin) } 
     before do 
      sign_in admin 
      visit users_path 
     end 

     it { should have_link('delete', href: user_path(User.first)) } 
     it "should be able to delete another user" do 
      expect { click_link('delete') }.to change(User, :count).by(-1) 
     end 
     it { should_not have_link('delete', href: user_path(admin)) } 
    end 
end 

describe "signup page" do 
    before { visit signup_path } 
    let(:submit) { "Create my account" } 

    it { should have_selector('h1', text: 'Sign up')} 
    it { should have_selector('title', text: full_title('Sign up'))} 

    describe "with invalid information" do 
     it "should not create a user" do 
      expect { click_button submit }.not_to change(User, :count) 
     end 

     describe "after submission" do 
      before { click_button submit } 

      it { should have_selector('title', text: 'Sign up') } 
      it { should have_content('error') } 
     end 
    end 

    describe "with valid information" do 
     before do 
      fill_in "Name", with: "Example User" 
      fill_in "Email", with: "[email protected]" 
      fill_in "Password", with: "foobar" 
      fill_in "Confirmation", with: "foobar" 
     end 

     it "should create a user" do 
      expect { click_button submit }.to change(User, :count).by(1) 
     end 

     describe "after saving the user" do 
      before { click_button submit } 
      let(:user) { User.find_by_email('[email protected]') } 

      it { should have_selector('title', text: user.name) } 
      it { should have_selector('div.alert.alert-success', text: 'Welcome') } 
     end 
    end 
end 

describe "profile page" do 
    # Code to make a user variable 
    let(:user) { FactoryGirl.create(:user) } 
    before { visit user_path(user) } 

    it { should have_selector('h1', text: user.name) } 
    it { should have_selector('title', text: user.name) } 
end 

describe "edit" do 
    let(:user) { FactoryGirl.create(:user) } 
    before do 
     sign_in user 
     visit edit_user_path(user) 
    end 

    describe "page" do 
     it { should have_selector('h1', text: "Update your profile") } 
     it { should have_selector('title', text: "Edit user") } 
     it { should have_link('change', href: 'http://gravatar.com/emails') } 
    end 

    describe "with invalid information" do 
     before { click_button "Save changes" } 
     it { should have_content('error') } 
    end 

    describe "with valid information" do 
     let(:new_name) { "New Name" } 
     let(:new_email) { "[email protected]" } 

     before do 
      fill_in "Name", with: new_name 
      fill_in "Email", with: new_email 
      fill_in "Password", with: user.password 
      fill_in "Confirm Password", with: user.password 
      click_button "Save changes" 
     end 

     it { should have_selector('title', text: new_name) } 
     it { should have_selector('div.alert.alert-success') } 
     it { should have_link('Sign out', href: signout_path) } 
     specify { user.reload.name.should == new_name } 
     specify { user.reload.email.should == new_email } 
    end 
end 
end 

這是我的database.yml文件:

# PostgreSQL. Versions 8.2 and up are supported. 
# 
# Install the pg driver: 
# gem install pg 
# On Mac OS X with macports: 
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config 
# On Windows: 
# gem install pg 
#  Choose the win32 build. 
#  Install PostgreSQL and put its /bin directory on your path. 
# 
# Configure Using Gemfile 
# gem 'pg' 
# 
development: 
    adapter: postgresql 
    encoding: unicode 
    database: rails 
    pool: 5 
    username: postgres 
    password: pwpwpwpw 

    # Connect on a TCP socket. Omitted by default since the client uses a 
    # domain socket that doesn't need configuration. Windows does not have 
    # domain sockets, so uncomment these lines. 
    #host: localhost 
    #port: 5432 

    # Schema search path. The server defaults to $user,public 
    #schema_search_path: myapp,sharedapp,public 

    # Minimum log levels, in increasing order: 
    # debug5, debug4, debug3, debug2, debug1, 
    # log, notice, warning, error, fatal, and panic 
    # The server defaults to notice. 
    #min_messages: warning 

# Warning: The database defined as "test" will be erased and 
# re-generated from your development database when you run "rake". 
# Do not set this db to the same as development or production. 
test: &test 
    adapter: postgresql 
    encoding: unicode 
    database: rails 
    pool: 5 
    username: postgres 
    password: pwpwpwpw 

production: 
    adapter: postgresql 
    encoding: unicode 
    database: rails 
    pool: 5 
    username: postgres 
    password: pwpwpwpw 

cucumber: 
    <<: *test 
+1

檢查你的'config/database.yml'。你有開發和測試環境的獨立數據庫嗎? – zetetic 2012-07-31 01:10:39

+0

我更新了原來的帖子,現在應該顯示我的database.yml文件。 – robins35 2012-07-31 01:12:19

+0

哦,你完全正確 – robins35 2012-07-31 01:14:25

回答

2

您需要開發和測試環境不同的數據庫。例如:

development: &BASE 
    adapter: postgresql 
    encoding: unicode 
    database: rails_development 
    pool: 5 
    username: postgres 
    password: pwpwpwpw 

test: &test 
    <<: *BASE 
    database: rails_test 

production: 
    <<: *BASE 
    database: rails_production 

cucumber: 
    <<: *test 
+0

你是對的,謝謝你指出。 – robins35 2012-08-01 19:03:53