开发者

Rails 3 controller test

开发者 https://www.devze.com 2023-04-01 20:14 出处:网络
I\'m writing an app with Rails 3. In my functional test, test开发者_如何学JAVA/functional/cust_infos_controller_test.rb, I have these:

I'm writing an app with Rails 3. In my functional test, test开发者_如何学JAVA/functional/cust_infos_controller_test.rb, I have these:

require 'test_helper'

class CustInfosControllerTest < ActionController::TestCase
  # Replace this with your real tests.
  test "should get cust" do
    get :customerinfo
    assert_response :success
    assert_not_nil assigns("cust_infos")
  end
end

My controller is fairly straightforward as it just finds all the customer's info:

class CustInfosController < ApplicationController
  def customerinfo
    @cust_info = CustInfo.find(:all, :order=>"cust_id")
    @cust_info.each do |ci|                                             
      if ci.upload_freq == 'W'
        ci.upload_freq = 'Weekly'
      elsif ci.upload_freq == 'M'
        ci.upload_freq = 'Monthly'
      end
    end
  end
end

When I ran it with:

$ ruby -Itest test/functional/cust_infos_controller_test.rb

I got the following error:

Loaded suite test/functional/cust_infos_controller_test
Started
E
Finished in 0.025258 seconds.

1) Error:
test_should_get_cust(CustInfosControllerTest):
ActiveRecord::StatementInvalid: PGError: ERROR:  numeric field overflow
DETAIL:  A field with precision 4, scale 0 must round to an absolute value less than 10^4.

: INSERT INTO "cust_infos" ("cust_id") VALUES (298486374)


1 tests, 0 assertions, 0 failures, 1 errors

In my cust_infos table, I have cust_id as integer. But I don't know why when I ran controller test to just get some record, active record will execute insert statement. How do I fix this?

UPDATE: I commented out all the lines in customerinfo method, and run the same test. Got the exact same result. So I'm guessing it's not my methods behavior, but rather Rails? Any hint?


I see what you are trying to do. What you need to do is create a helper for your view.

application_helper.rb

def display_upload_freq(s)
  case s
  when 'W'
    'Weekly'
  when 'M'
    'Monthly'
  end
end

cust_info_controller.rb

class CustInfosController < ApplicationController
  def customerinfo
    @cust_info = CustInfo.find(:all, :order=>"cust_id")
  end
end

Then in your view when your are iterating through @cust_info, for upload_freq, use dislay_upload_freq(ci.upload_freq). Assuming you have @cust_info.each do |ci|. Then you won't be confusing the db with saving anything.


Are you using factories or fixtures to create your test data? If not, how are you creating it? My guess is that the insert is happening when your test data is being set up, not because of anything happening in your controller. The second part of that guess is borne out by the fact that commenting out all code in your controller isn't getting rid of the error.


I know this post is quite old, but I will post my answer in case someone is brought here by search engines. I had the exact same problem, the issue originates from the fixture file(s) in test/fixtures/{name_of_your_model}.yml. Rails adds some initial values in this file, in my case it looked like this:

one: {}
# column: value
#
two: {}
#  column: value

and when you want to run the test, it will try to create the test database using this fixtures. And that's where the issue occurs. Trying to create an empty record while you did not allow a null ID in your table.

Deleting these fixtures, or filling them with appropriate values should solve the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消