简体   繁体   中英

Rails Rspec Test Resets Database

I'm doing the Rails tutorial, and have been following it fairly strictly. However, my user_pages_spec.rb test resets the development database. So whenever I run a test, I lose all of the current data that was in there before the test. My guess is that the problem is on line 11, but I'm not really sure. Any help is greatly appreciated, thanks :)

Here's my user_pages_spec.rb file:

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: "user@example.com"
            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('user@example.com') }

            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) { "new@example.com" }

        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

Here's my database.yml file:

# 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

You need separate databases for development and test environments. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM