【发布时间】:2020-05-07 12:07:17
【问题描述】:
我正在做一个作业,我根据我们的说明编写了以下方法:
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)>'
这是 rspec 测试:
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.today.to_s(:long)分配给本地变量due_date,但不再从该变量中读取。 -
抱歉,我已经添加了
-
params[:due_date]没有使用due_date。它们是两个不同的变量。另外,上面写着expected: Thu, 07 May 2020 - got: "2020-05-07可能需要手动转换一下?
标签: ruby-on-rails ruby rspec rspec-rails