2016-04-05 5 views
0

私は次の表のtodolistの持っている:私はCRUDメソッド作成なぜ私のルビーテストは合格しないのですか?

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 

を:

def create_todolist(params) 
     todolist = TodoList.create(params) 
    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 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を変更してキーの値を変更するには?

答えて

2

あなたのフィールドはlist_nameと呼ばれますが、:name => 'myList'を渡しています。 due_datelist_due_dateと同じです。作成した `使うよりよい` create`の戻り値をチェックしていない場合

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

@Pracedeする必要があります! '、これは別の質問で検証エラーなど – Vasfed

+1

の場合に例外が発生します。あなたはVasfedの答えを受け入れ、別の質問を投稿するべきです。 – SteveTurczyn

関連する問題