【问题标题】:rake test errors from deleted table column从已删除的表列中提取测试错误
【发布时间】:2013-07-04 08:41:30
【问题描述】:

所以我为我的 ruby​​ on rails 项目生成了一个脚手架,结果发现我添加了一个不再需要的列。

我生成了一个迁移并删除了额外的列。

然后我删除了代码中所有调用它的列。

但是当我运行“rake 测试”时,我遇到了几个错误,它们似乎指向这个不存在的列,我修改了 controller_test.rb

  @foo = foos(:one)
  @update = {
  title:       'Lorem Ipsum',
  description: 'Wibbles are fun!',
  image_url:   'lorem.jpg',
  start_date:  '12/12/13',
  end_date:    '13/12/13'
  }

我还修改了应该创建测试和应该更新测试

 test "should create foo" do
 assert_difference('Foo.count') do
  post :create, foo: @update 
 end

还有这个

 test "should update foo" do
 put :update, id: @foo, foo: @update 
 assert_redirected_to foo_path(assigns(:foo))
 end

任何帮助将不胜感激,如果需要更多信息,我可以提供更多

【问题讨论】:

    标签: ruby-on-rails ruby sqlite rake-test


    【解决方案1】:

    经过几个小时的坚持,我发现了问题。

    虽然该列已从表中删除,但项目中仍有对其的引用,在我的情况下,它位于 test/fixtures/foo.yaml

    要查找所提到的特定行(如果您的 IDE 没有搜索功能),请转到您工作并运行的 git bash

     $ grep -r "search term" *
    

    它挑选出搜索词所在的目录,然后它只是一个简单的例子,只是删除它的所有引用

    请注意,此命令会在底部分配您要查找的行。

    【讨论】:

      【解决方案2】:

      您是 Rails 新手吗?如果是的话,你能告诉我你在遵循什么指南吗?以及你是如何生成脚手架的

      Scaffold 创建了所有这些文件

        invoke  active_record
        create    db/migrate/20130704111616_create_products.rb
        create    app/models/product.rb
        invoke    test_unit
        create      test/models/product_test.rb
        create      test/fixtures/products.yml
        invoke  resource_route
         route    resources :products
        invoke  scaffold_controller
        create    app/controllers/products_controller.rb
        invoke    erb
        create      app/views/products
        create      app/views/products/index.html.erb
        create      app/views/products/edit.html.erb
        create      app/views/products/show.html.erb
        create      app/views/products/new.html.erb
        create      app/views/products/_form.html.erb
        invoke    test_unit
        create      test/controllers/products_controller_test.rb
        invoke    helper
        create      app/helpers/products_helper.rb
        invoke      test_unit
        create        test/helpers/products_helper_test.rb
        invoke    jbuilder
        create      app/views/products/index.json.jbuilder
        create      app/views/products/show.json.jbuilder
        invoke  assets
        invoke    coffee
        create      app/assets/javascripts/products.js.coffee
        invoke    scss
        create      app/assets/stylesheets/products.css.scss
        invoke  scss
        create    app/assets/stylesheets/scaffolds.css.scss
      

      由 active_record、scaffold_controller 和 test_unit 创建的文件可能会在测试中导致错误尝试检查它们以查看是否有任何不应该存在的内容。

      还可以尝试删除 development.sqlite3 和 test.sqlite3 并再次运行 rake db:migrate

      还有rails destroy 命令将删除所有自动生成的文件

      【讨论】:

      • 感谢您的意见,但我昨晚设法解决了问题
      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2019-08-10
      • 2015-09-23
      • 2018-05-15
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多