测试数据库上的资源ID与rspec运行不匹配

时间:2015-12-22 19:09:06

标签: ruby-on-rails rspec

当我运行rspec时,我发现以下错误

Module #4: Navigation Tests rq05 rq05d TodoItem endpoint is accessible (show and destroy) through TodoList
Failure/Error: expect(page).to have_link('Show', :href => "#{todo_list_todo_item_path(tl_id, l.id)}")
expected #has_link?("Show", {:href=>"/todo_lists/832/todo_items/2538"}) to return true, got false
# ./spec/nested_resources_spec.rb:69:in `block (5 levels) in <top (required)>'
 # /Users/subratrout/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/relation/delegation.rb:46:in `each'
 # /Users/subratrout/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/relation/delegation.rb:46:in `each'
 # ./spec/nested_resources_spec.rb:67:in `block (4 levels) in <top (required)>'
 # ./spec/nested_resources_spec.rb:14:in `block (2 levels) in <top (required)>'

我在页面上有所需的show链接。当我检查我的测试数据库时,我发现我有不同id号的todo_list,所以测试没有通过。 每次运行rspec时,都会删除todo_list集,并在测试数据库中创建一组新的todo_lists,创建不同的主ID号。因此,测试会查找todo_lists的先前ID并且不会通过。

My seeds.db如下:

today = Date.today
two_days_ago = Date.today - 2.days
three_days_ago = Date.today - 3.days
dates = [today, two_days_ago, three_days_ago]

User.destroy_all
TodoList.destroy_all

100.times { |index| TodoList.create! list_name: "List #{index}", list_due_date: dates.sample }

TodoList.all.each do |list|
list.todo_items.create! [
{ title: "Task 1", due_date: dates.sample, description: "very important task TEST", completed: false },
{ title: "Task 2", due_date: dates.sample, description: "do something else TEST", completed: true},
{ title: "Task 3", due_date: dates.sample, description: "learn Action Pack TEST", completed: true}
 ]
end

users = User.create! [
{ username: "jim", password: "abc123" },
{ username: "rich", password: "123abc" }
]

TodoList.all.each do |list|
list.user = users.sample
list.save!
end

我的rspec规范文件如下:

context "rq05d" do

scenario "TodoItem endpoint is accessible (show and destroy) through TodoList" do
            tl_id = TodoList.all.sample.id
            list_items = TodoList.find(tl_id).todo_items
            # Confirm links in list page are nested and that the
            # show page results in expected method behavior
            list_items.each do |l|
                visit(todo_list_path(tl_id))
                expect(page).to have_link('Show', :href => "#{todo_list_todo_item_path(tl_id, l.id)}")
                expect(page).to have_link('Destroy', :href => "#{todo_list_todo_item_path(tl_id, l.id)}")
            end
        end

    end

用于Todo_list文件的show.html.erb:

<h1>Listing Todo Items</h1>

<table>
<thead>
<tr>
  <th>Due date</th>
  <th>Title</th>
  <th>Description</th>
  <th>Completed</th>
  <th colspan="2"></th>
</tr>

<tbody>
<% @todo_list.todo_items.each do |todo_item| %>
  <tr>
    <td><%= todo_item.due_date %></td>
    <td><%= todo_item.title %></td>
    <td><%= todo_item.description %></td>
    <td><%= todo_item.completed %></td>
    <td><%= link_to 'Show', [@todo_list, todo_item] %></td>
    <td><%= link_to 'Destroy', [@todo_list, todo_item], method: :delete %></td>
  </tr>
<% end %>

我的问题是当我使用sqlitebrowser检查我的测试数据库时,我没有看到主要ID为832的todo_list和主要ID为2538的todo_items / todo_lists / 832 / todo_items / 2538。相反,我看到主要ID在900之后为todo_lists开始,而todo_items则从2700开始。这是测试返回false值而不是传递的原因吗?

0 个答案:

没有答案