如何使用此ActiveRecord关联编写测试

时间:2019-01-11 11:50:17

标签: ruby-on-rails minitest

我有一个Task模型,该模型具有多个参数,其中一个参数是user_ids,该参数允许将多个用户添加到一个任务中。这是通过has_many: :users, through: assigned_users

完成的

实际的控制器操作正常,但我正在重构以具有更好的测试范围。我可以将任务保存在应用程序中,但测试始终会抛出此错误(下面是完整的堆栈跟踪信息):

Error:
TaskTest#test_task_can_save:
TypeError: no implicit conversion of Symbol into Integer
    test/models/task_test.rb:14:in `block in <class:TaskTest>'

一个非常基本的测试应该是任务可以保存:

需要“ test_helper”

class TaskTest < ActiveSupport::TestCase
  def setup
    @usr = users :joe

    @task = Task.new(comments: "Basic task", project_id: 1, subproject_id: 2,
                      day: "2019-01-09", time: 55, user_ids: [@usr.id])
  end



   test "task can save" do
     assert @task.save "task should save"
   end
  end

这是有效 create的操作。

tasks_controller

def create

@task = current_user.tasks.build(task_params)

respond_to do |format|
  if @task.save
    current_user.tasks << @task
    @tasks = current_user.tasks
    format.js {flash[:notice] = "Task added"}

   else
     ...

end

private
  def task_params
     params.require(:task).permit(:comments, :project_id, :subproject_id, :day, :time, user_ids: [])
 end

保存任务,一切正常,只是不进行此测试。我在user_ids:哈希中尝试了其他操作,例如[User.first.id],而[1]似乎都给出了相同的错误。

un options: --seed 2048---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=] 0% Time: 00:00:00,  ETA: ??:??:??

# Running:

ERROR["test_task_can_save", #<Minitest::Reporters::Suite:0x007f8f9016b3c8 @name="TaskTest">, 0.38674699992407113]
 test_task_can_save#TaskTest (0.39s)
TypeError:         TypeError: no implicit conversion of Symbol into Integer
            test/models/task_test.rb:14:in `block in <class:TaskTest>'

  1/1: [==========================================================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00
E

Error:
TaskTest#test_task_can_save:
TypeError: no implicit conversion of Symbol into Integer
    test/models/task_test.rb:14:in `block in <class:TaskTest>'


/usr/local/lib/ruby/gems/2.4.0/gems/railties-5.0.7.1/lib/rails/test_unit/reporter.rb:70:in `method': undefined method `test_task_can_save' for class `Minitest::Result' (NameError)
    from /usr/local/lib/ruby/gems/2.4.0/gems/railties-5.0.7.1/lib/rails/test_unit/reporter.rb:70:in `format_rerun_snippet'
    from /usr/local/lib/ruby/gems/2.4.0/gems/railties-5.0.7.1/lib/rails/test_unit/reporter.rb:23:in `record'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:803:in `block in record'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:802:in `each'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:802:in `record'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:334:in `run_one_method'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:321:in `block (2 levels) in run'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:320:in `each'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:320:in `block in run'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:360:in `on_signal'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:347:in `with_info_handler'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:319:in `run'
    from /usr/local/lib/ruby/gems/2.4.0/gems/railties-5.0.7.1/lib/rails/test_unit/line_filtering.rb:9:in `run'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:159:in `block in __run'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:159:in `map'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:159:in `__run'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:136:in `run'
    from /usr/local/lib/ruby/gems/2.4.0/gems/minitest-5.11.3/lib/minitest.rb:63:in `block in autorun'
    from /usr/local/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork'
    from /usr/local/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:171:in `serve'
    from /usr/local/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
    from /usr/local/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
    from /usr/local/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
    from /usr/local/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

1 个答案:

答案 0 :(得分:0)

结果是exports.confirmOrder = functions.https.onCall((data, context) => __awaiter(this, void 0, void 0, function* () { if(validateData()){ yield new Promise((reject, resolve) => { return verifychecksum(data,paytm_config.MERCHANT_KEY); }).then((result) => __awaiter(this, void 0, void 0, function* () { if (result) { const doc = yield db.collection(targetColRef).doc(data.TARGET_ID).collection('orders').doc(data.ORDER_ID).get(); const time = getCurrentDateNumber().time; if (doc.exists && (time - doc.orderTime) < 300) { yield db.collection(targetColRef).doc(data.TARGET_ID).collection('orders').doc(data.ORDER_ID).update({ status: 'pending' }); } else { //doc not exists console.log("Order not exist Or time diff more than 300 sec ! /DATA=>" + JSON.stringify(data) + "/Context=>" + JSON.stringify(context)); throw new functions.https.HttpsError('permission-denied', 'Time Out!'); } } else { // result==false console.log("Failed to verify checksum! /DATA=>" + JSON.stringify(data) + "/Context=>" + JSON.stringify(context)); throw new functions.https.HttpsError('permission-denied', 'bad request'); } //.. })).catch((err) => { throw new functions.https.HttpsError('permission-denied', err); }); console.log("successfull"); return "successfull"; }else{ //.... }})): 存在问题,因为我没有使用project_id:方法设置项目。

我不得不从项目固定装置中抽出setup

Project