2016-04-05 18 views
0

Aşağıdaki tabloda todolist vardır: Neden ruby ​​testlerim geçmiyor?

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 

ben rezil yöntemleri oluşturmak:

def create_todolist(params) 
     todolist = TodoList.create(params) 
    end 

Ve followging testler var: i başlattığınızda

 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 

testi bana vermesini takip eden hatalar:

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 

Çünkü bence params öznitelikleri tam olarak aynı TodoList öznitelikleriyle eşleşmiyor. Anahtar değerlerini değiştirmek için create_todolist'imi nasıl değiştirebilirim?

cevap

2

Tarla list_name olarak adlandırılıyor, ancak :name => 'myList' geçiyorsunuz. due_date ve list_due_date için de geçerlidir. Eğer dönüş değeri kontrol etmiyor iseniz

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

@Pracede olmalı 'daha iyi şekilde' oluşturmak create'! ', bu Bu farklı bir sorudur doğrulama hataları vb – Vasfed

+1

durumunda istisna yükseltecektir. Vasfed'in cevabını kabul etmeli ve başka bir soru sormalısınız. – SteveTurczyn

İlgili konular