为什么我的红宝石测试没有通过?

时间:2016-04-05 15:39:32

标签: ruby rspec

我有下表TodoList:

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(params)
  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

我认为这是因为params属性与完全相同的TodoList属性不匹配。如何修改我的create_todolist来更改键值?

1 个答案:

答案 0 :(得分:2)

您的字段被称为list_name,但您正在通过:name => 'myList'due_datelist_due_date也是如此。

应该是

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