Topic: Database Issue

I am having a really weird problem with my MySQL database. I have a test that looks for an item in my database with an id of 1 and my database shows this. When I run the functional test for it though I get an error showing that the test can't find anything in the database with an id of 1. When I look at my database now it shows the id of the previous item as 5 now.
I tried then to change the id in my test from 1 to 5 and run the functional test again and now it says it can't find anything in the database with an id of 5. When I check the database it now shows that the id of 1 instead of 5.
I am new to rails and databases so any help in this issue would be greatly appreciated. I am not sure if this is relevant or not but I don't have a seperate version of mysql it is bundled in with Xampp for windows.
I hope I have explained this confusing issue well enough.
Thanks

Re: Database Issue

Are you using fixtures? How are you creating the item with the id which you are looking up?

Railscasts - Free Ruby on Rails Screencasts

Re: Database Issue

I am using fixture files and the data is being loaded by calling it from within the test file. I only have 4 items within the fixture file. My thinking is that it shouldn't even show something with an id of 5 in the database, should it?

Re: Database Issue

If one test ends up creating and saving a model then this is probably what is making the 5th id.

Would you mind posting the fixture file and the test case?

Railscasts - Free Ruby on Rails Screencasts

Re: Database Issue

Here is the fixtures file called authors.yml

joel_spolsky:
  id: 1
  first_name: Joel
  last_name: Spolsky
jeremy_keith:
  id: 2
  first_name: Jeremy
  last_name: Keith
christian_hellsten:
  id: 3
  first_name: Christian
  last_name: Hellsten
jarkko_laine:
  id: 4
  first_name: Jarkko
  last_name: Laine

And here is the test file:

require File.dirname(__FILE__) + '/../../test_helper'
require 'admin/author_controller'

# Re-raise errors caught by the controller.
class Admin::AuthorController; def rescue_action(e) raise e end; end

class Admin::AuthorControllerTest < Test::Unit::TestCase
  fixtures :authors

  def setup
    @controller = Admin::AuthorController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  def test_new
    get :new
    assert_template 'admin/author/new'
    assert_tag 'h1', :content => 'Create new author'
    assert_tag 'form', :attributes => {:action => '/admin/author/create'}
  end
 
  def test_create
    get :new
    assert_template 'admin/author/new'
    assert_difference(Author, :count)do
      post :create, :author => {:first_name => 'Joel',
                                :last_name => 'Spolsky'}
      assert_response :redirect
      assert_redirected_to :action => 'index'
  end
  assert_equal 'Author Joel Spolsky was successfully created.', flash[:notice]
  end
 
  def test_failing_create
    assert_no_difference(Author, :count) do
      post :create, :author => {:first_name => 'Joel'}
      assert_response :success
      assert_template 'admin/author/new'
      assert_tag :tag => 'div', :attributes => {:class => 'fieldWithErrors'}
    end
  end
 
def test_index
    get :index
    assert_response :success
    assert_tag  :tag => 'table',
                :children => { :count => Author.count + 1,
                               :only => {:tag => 'tr'} }
    Author.find(:all).each do |a|
      assert_tag :tag => 'td',
              :content => /Joel/
      assert_tag :tag => 'td',
                 :content => /Spolsky/
    end
  end
 
  def test_show
    get :show, :id => 1
    assert_template 'admin/author/show'
    assert_equal 'Joel', assigns(:author).first_name
    assert_equal 'Spolsky', assigns(:author).last_name
  end
 
  def test_edit
    get :edit, :id => 1
    assert_tag :tag => 'input',
               :attributes => { :name => 'author[first_name]',
                                :value => 'Joel' }
    assert_tag :tag => 'input',
               :attributes => { :name => 'author[last_name]',
                                :value => 'Spolsky' }
    end
   
  def test_update
    post :update, :id => 1, :author => { :first_name => 'Joseph',
                                         :last_name => 'Spolsky' }
    assert_response :redirect
    assert_redirected_to :action => 'show', :id => 1
    assert_equal 'Joseph', Author.find(1).first_name
  end
 
  def test_destroy
    assert_difference(Author, :count, -1) do
      post :destroy, :id => 1
      assert_response :redirect
      assert_redirected_to :action => 'index'
      follow_redirect
      assert_tag :tag => 'div', :attributes => {:id => 'notice'},
                 :content => /Successfully deleted author Joel Spolsky/
      end
  end
 
end


Thanks for all your help Ryan, eventually I will learn enough to start helping others instead of just asking questions.

Re: Database Issue

Can you provide the details on the error? Which test is failing? A stack trace would be nice too if you are getting one.

Railscasts - Free Ruby on Rails Screencasts

Re: Database Issue

I am now on a different computer and am trying the same application. I no longer am getting the errors that I was previously and everything seems to be working fine. The only thing different between systems is that I am using XAMPP on the system with the errors and on the system without the errors I am using WampServer. They contain different versions of MySQL and PHP but other than that function identically. Would this make a difference when developing with rails?

Re: Database Issue

Fixtures can sometimes cause this kind of problem where it works on one computer but not another. But your fixtures usage looks correct and is pretty basic.

Try running this command in your project directory:

rake db:test:clone

If this doesn't fix it, it may be the XAMPP install as you said - unfortunately I don't have experience with this so I don't really know what to suggest.

Railscasts - Free Ruby on Rails Screencasts

Re: Database Issue

The problem was the difference in the software packages used to install apache/php/mysql. After uninstalling XAMPP and in stallwing WAMP on the other machine everything appears to be working as it should.
Thanks for the help.