0
我有以下表TodoList的:爲什麼create方法不會返回帶id的對象?
class TodoList < ActiveRecord::Base
end
和
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
我創建的CRUD方法:
def create_todolist(params)
todolist = TodoList.create(list_name:params[:name], list_due_date: params[:due_date])
end
和我有followging測試:
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 rq03 rq03.2 assignment code has create_todolist method should create_todolist with provided parameters:
Failure/Error:
expect(testList.id).not_to be_nil NoMethodError: undefined method id' for nil:NilClass
# ./spec/assignment_spec.rb:173:in block (4 levels) in <top (required)>'
# ./spec/assignment_spec.rb:14:in `block (2 levels) in <top (required)>'
看來,創建方法並不成功。請問有什麼問題?
我認爲在'TodoList.find_by list_name:'mylist''中可能會有不必要的':'' –
@pracede您能否顯示'TodoList'模型的代碼? –
@MaximPontyushenko我添加了TodoList模型 – Pracede