我在Rails 3中使用minitest运行一些测试。我有一个需求模型和一个ind_requirements模型,它作为行业模型和需求模型之间的交集。我在需求模型上的has_many上有一个依赖:destroy set,但删除它似乎对我所看到的内容没有任何影响。
这是所有控制器测试,所以我没有与用户界面进行交互,或者我会怀疑这里报告的内容:Active record update_attribute executes a DELETE query on an associated object(child) of an object(parent) when parent is updated
当命中@ requirement.update_attributes时,我在日志中得到以下输出:
Processing by RequirementsController#update as HTML
Parameters: {"requirement"=>{"reqText"=>"second version", "reqTitle"=>"Second Version"}, "id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
SQL (0.1ms) SELECT "requirements"."id" AS t0_r0, "requirements"."reqTitle" AS t0_r1, "requirements"."reqText" AS t0_r2, "requirements"."created_at" AS t0_r3, "requirements"."updated_at" AS t0_r4, "requirements"."category_id" AS t0_r5, "requirements"."status" AS t0_r6, "requirements"."source_id" AS t0_r7, "requirements"."sortOrder" AS t0_r8, "requirements"."version" AS t0_r9, "requirements"."active" AS t0_r10, "categories"."id" AS t1_r0, "categories"."catName" AS t1_r1, "categories"."created_at" AS t1_r2, "categories"."updated_at" AS t1_r3, "categories"."catAbbr" AS t1_r4 FROM "requirements" LEFT OUTER JOIN "categories" ON "categories"."id" = "requirements"."category_id" WHERE "requirements"."active" = 't' AND "requirements"."id" = ? ORDER BY categories.catName LIMIT 1 [["id", "1"]]
(0.2ms) SELECT COUNT(*) FROM "roles" INNER JOIN "role_assignments" ON "roles"."id" = "role_assignments"."role_id" WHERE "role_assignments"."user_id" = 1 AND (roleName = 'Editor')
(0.2ms) SELECT COUNT(*) FROM "roles" INNER JOIN "role_assignments" ON "roles"."id" = "role_assignments"."role_id" WHERE "role_assignments"."user_id" = 1 AND (roleName = 'Administrator')
(0.2ms) SAVEPOINT active_record_1
Industry Load (0.2ms) SELECT "industries".* FROM "industries" INNER JOIN "ind_requirements" ON "industries"."id" = "ind_requirements"."industry_id" WHERE "ind_requirements"."requirement_id" = 1 ORDER BY indName
SQL (0.2ms) DELETE FROM "ind_requirements" WHERE "ind_requirements"."requirement_id" = 1 AND "ind_requirements"."industry_id" = 2
SQL (0.5ms) INSERT INTO "requirement_versions" ("category_id", "created_at", "reqText", "reqTitle", "req_id", "status", "updated_at", "version") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["category_id", nil], ["created_at", Wed, 12 Mar 2014 06:00:13 UTC +00:00], ["reqText", "This is a test general requirement for the purpose of, um, testing things"], ["reqTitle", "Test Requirement 19"], ["req_id", 1], ["status", "Public"], ["updated_at", Wed, 12 Mar 2014 06:00:13 UTC +00:00], ["version", 1]]
(0.1ms) UPDATE "requirements" SET "reqText" = 'second version', "reqTitle" = 'Second Version', "version" = 2, "updated_at" = '2014-03-12 06:00:13.418730' WHERE "requirements"."id" = 1
(0.1ms) RELEASE SAVEPOINT active_record_1
注意DELETE FROM。这会打破我的测试,因为一旦从测试数据库中删除这些记录,我的后续代码就无法正常工作。在开发中运行时,仅在测试中不会触发此删除。
我是否缺少配置选项?
这是requirements.rb文件(没有其他所有方法都不属于此部分):
class Requirement < ActiveRecord::Base
has_many :ind_requirements, dependent: :destroy
has_many :requirement_versions
has_many :industries, through: :ind_requirements
has_many :responses
belongs_to :category
scope :active, where(:active => true)
default_scope active.includes(:category).order('categories.catName')
scope :submitted, where(:status => :Submitted)
scope :public, where(:status => :Public)
accepts_nested_attributes_for :requirement_versions
attr_accessible :reqText, :reqTitle, :industry_ids, :category_id, :catName, :status, :user_id, :source_id, :catAbbr, :reqNumber, :sortOrder, :requirement_versions_attributes
以下是实际测试用例:
test "When a requirement is reverted, the previous associated industry list is restored" do
login_admin
@requirement = create(:requirement)
@industry = create(:industry, indName: "First Industry")
@indLink = create(:ind_requirement, requirement_id: @requirement.id)
@indLink.save!
put :update, id: @requirement, requirement: { reqText: "second version", reqTitle: "Second Version"}
@industry = create(:industry, indName: "Second Industry")
@indLink.industry_id = @industry.id
@indLink.save!
get :revert, id: @requirement
@requirement = Requirement.find(@requirement.id)
@indLink = @requirement.ind_requirements.first
assert_equal(@indLink.industry.indName, "First Industry")
end
答案 0 :(得分:0)
问题在于,当我向需求发送更新时,我只发送更新的参数:put:update,id:@requirement,requirement:{reqText:“second version”,reqTitle:“Second Version” ,但不包括复选框的参数,因为我没有修改它们。不幸的是,最终结果是行业被删除了。我想是因为缺少参数并不是因为它们没有被更新,而是被删除了。