我正在做一项任务,我根据我们的指示编写了以下方法:
def create_todolist(params)
due_date = Date.today.to_s(:long)
TodoList.create(list_name: params[:name],list_due_date: params[:due_date])
end
但是当我运行rspec测试时,我会得到以下错误:
1) Assignment rq03 rq03.2 assignment code has create_todolist method should create_todolist with provided parameters
Failure/Error: expect(testList.list_due_date).to eq due_date
expected: Thu, 07 May 2020
got: "2020-05-07"
(compared using ==)
Diff:
@@ -1,2 +1,2 @@
-Thu, 07 May 2020
+"2020-05-07"
# ./spec/assignment_spec.rb:177:in `block (4 levels) in <top (required)>'
# ./spec/assignment_spec.rb:14:in `block (2 levels) in <top (required)>'
以下是rsspec测试:
context "rq03.2 assignment code has 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
起初,我只有due_date = Date.today
,遇到了同样的错误,我不知道如何修复它。我想知道这是否是因为我使用的ruby/rails版本与创建课程时使用的版本不同(5年前-_-(。
如有任何帮助,我们将不胜感激!
谢谢:(
您正在尝试比较Date对象:
due_date = Date.today
使用您在创建记录时生成的字符串对象:
Date.today.to_s(:long)
正如你所看到的,这些是不同类型的对象:
Date.today.to_s(:long)
=> "May 07, 2020"
Date.today.to_s(:long).class
=> String
Date.today
=> 2020-05-07
Date.today.class
=> Date
Date.today.to_s(:long) == Date.today
=> false
我想明白了。当我创建TodoLists表时,我没有将迁移类型指定为:date。因此,默认情况下,duedate被设置为字符串。所以我设置为键入:date,并将due_date更改为equal:
due_date = Date.today
谢谢你花时间帮助我:(