为什么我的ruby测试没有通过?

为什么我的ruby测试没有通过?,ruby,rspec,Ruby,Rspec,我有下表要列出: class CreateTodoLists < ActiveRecord::Migration def change create_table :todo_lists do |t| t.string :list_name t.date :list_due_date t.timestamps null: false end end end 我有以下测试: context "the code has to

我有下表要列出:

class CreateTodoLists < ActiveRecord::Migration
  def change
    create_table :todo_lists do |t|
      t.string :list_name
      t.date :list_due_date
      t.timestamps null: false
    end
  end
end
我有以下测试:

     context "the code has to create_todolist method" do
        it { is_expected.to respond_to(:create_todolist) } 
        it "should create_todolist with provided parameters" do
            expect(TodoList.find_by list_name: "mylist").to be_nil
            due_date=Date.today
            assignment.create_todolist(:name=> 'mylist', :due_date=>due_date)
            testList = TodoList.find_by list_name: 'mylist'
            expect(testList.id).not_to be_nil
            expect(testList.list_name).to eq "mylist"
            expect(testList.list_due_date).to eq due_date
            expect(testList.created_at).not_to be_nil
            expect(testList.updated_at).not_to be_nil
        end  

  end
启动测试时,请给出以下错误:

assignment code has create_todolist method should create_todolist with provided parameters
     Failure/Error: assignment.create_todolist(:name=> 'mylist', :due_date=>due_date)

     ActiveRecord::UnknownAttributeError:
       unknown attribute 'name' for TodoList.
     # ./assignment/assignment.rb:25:in `create_todolist'
     # ./spec/assignment_spec.rb:171:in `block (4 levels) in <top (required)>'
     # ./spec/assignment_spec.rb:14:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # NoMethodError:
     #   undefined method `name=' for #<TodoList:0x007f96dd0d13f0>
     #   ./assignment/assignment.rb:25:in `create_todolist'

Finished in 0.14136 seconds (files took 1.66 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./spec/assignment_spec.rb:168 # Assignment rq03 rq03.2 assignment code has create_todolist method should create_todolist with provided parameters
分配代码已创建\u todolist方法应使用提供的参数创建\u todolist
失败/错误:分配。创建任务列表(:name=>mylist',:due\u date=>due\u date)
ActiveRecord::UnknownAttributeError:
TodoList的未知属性“name”。
#/assignment/assignment.rb:25:in'create_todolist'
#./spec/assignment_spec.rb:171:in'block(4层)in'
#./spec/assignment_spec.rb:14:in'block(2层)in'
# ------------------
#---原因:---
#命名错误:
#未定义的方法“name=”用于#
#/assignment/assignment.rb:25:in'create_todolist'
完成时间为0.14136秒(文件加载时间为1.66秒)
2例,1例失败
失败的示例:
rspec./spec/assignment_spec.rb:168#assignment rq03 rq03.2分配代码已创建_todolist方法应使用提供的参数创建_todolist

我认为这是因为params属性与TodoList属性不完全匹配。如何修改我的创建列表以更改键值?

您的字段名为
list\u name
,但您正在传递
:name=>“myList”
。 同样适用于
到期日
列表到期日

应该是

assignment.create_todolist(list_name:'mylist', list_due_date: due_date)

您的字段名为
list\u name
,但您正在传递
:name=>“myList”
。 同样适用于
到期日
列表到期日

应该是

assignment.create_todolist(list_name:'mylist', list_due_date: due_date)

@如果您没有检查
create
的返回值,请使用
create,它将在验证错误的情况下引发异常。这是另一个问题。您应该接受Vasfed的回答并发布另一个问题。@praced如果您没有检查
create
的返回值,请使用
create,它将在验证错误的情况下引发异常。这是另一个问题。你应该接受Vasfed的回答,然后发布另一个问题。