问题
I was developing sample project of Rails tutorial (M. Hartl) but at the end of chapter 9 I got failure for integration testing.
Assertion of test is like below:
assert_select 'a[href=?]', user_path(user), text: user.name
Body of error is:
FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 2016-01-25 23:03:39 +0330]
test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1453750419.70s)
Expected at least 1 element matching "a[href="/users/157312430"]", found 0..
Expected 0 to be >= 1.
So used byebug for debugging of test but not found any problem with it. Response of server to test module is as here (Result of "JSON.parse(response.body)"):
<html>
<head>
<title>All users | Ruby on Rails Tutorial Sample App</title>
<link rel="stylesheet" media="all" href="/assets/application-22ded774de18638e5d5df16d8f71a98d1ef8dd1443b345f205c4312455504d14.css" data-turbolinks-track="true" />
<script src="/assets/application-5f571eb3f10beb2509afac14df9da5d9dcd4afe9869ec7dcbc032ff50364d876.js" data-turbolinks-track="true"></script>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
</script>
<![endif]-->
</head>
<body>
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<a id="logo" href="/">sample app</a>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><a href="/">Home</a></li>
<li><a href="/help">Help</a></li>
<li><a href="/users">Users</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/users/762146111">Profile</a></li>
<li><a href="/users/762146111/edit">Settings</a></li>
<li class="divider"></li>
<li>
<a rel="nofollow" data-method="delete" href="/logout">Log out</a> </li>
</ul>
</li>
</ul>
</nav>
</div>
</header>
<div class="container">
<h1>All users</h1>
<div class="pagination"><ul class="pagination"><li class="prev previous_page disabled"><a href="#">← Previous</a></li> <li class="active"><a rel="start" href="/users?page=1">1</a></li> <li><a rel="next" href="/users?page=2">2</a></li> <li class="next next_page "><a rel="next" href="/users?page=2">Next →</a></li></ul></div>
<ul class="users">
<li>
<img alt="User 19" class="gravatar" src="https://secure.gravatar.com/avatar/9538140816b147f95b24b9cf9e052657?s=50" />
<a href="/users/14035331">User 19</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/14035331">delete</a>
</li><li>
<img alt="User 15" class="gravatar" src="https://secure.gravatar.com/avatar/00ef7603e06e47d4a56d219f777b4f8c?s=50" />
<a href="/users/157312430">User 15</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/157312430">delete</a>
</li><li>
<img alt="User 7" class="gravatar" src="https://secure.gravatar.com/avatar/77390d563208e0b52a1b5738ad5ab4ba?s=50" />
<a href="/users/172816471">User 7</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/172816471">delete</a>
</li><li>
<img alt="User 23" class="gravatar" src="https://secure.gravatar.com/avatar/c362e2d6797a6296bb3f81e2aeb9d588?s=50" />
<a href="/users/187601757">User 23</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/187601757">delete</a>
</li><li>
<img alt="User 27" class="gravatar" src="https://secure.gravatar.com/avatar/62506fe0a7f45e009116c1929ba91175?s=50" />
<a href="/users/205739844">User 27</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/205739844">delete</a>
</li><li>
<img alt="User 3" class="gravatar" src="https://secure.gravatar.com/avatar/4c42f5c3c13f092769cb0a695acc3d63?s=50" />
<a href="/users/220281934">User 3</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/220281934">delete</a>
</li><li>
<img alt="User 11" class="gravatar" src="https://secure.gravatar.com/avatar/637f1bf55f5d21863f52d5c4f1ac5c62?s=50" />
<a href="/users/235774389">User 11</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/235774389">delete</a>
</li><li>
<img alt="User 16" class="gravatar" src="https://secure.gravatar.com/avatar/abe3d2fffb6bb035d84880dc2da9ad87?s=50" />
<a href="/users/275330066">User 16</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/275330066">delete</a>
</li><li>
<img alt="User 20" class="gravatar" src="https://secure.gravatar.com/avatar/1b65d049a825d7f33e9e3250d6f773c3?s=50" />
<a href="/users/304595689">User 20</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/304595689">delete</a>
</li><li>
<img alt="User 4" class="gravatar" src="https://secure.gravatar.com/avatar/2462cf807167af6128cde02fc8f000f0?s=50" />
<a href="/users/323332591">User 4</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/323332591">delete</a>
</li><li>
<img alt="User 0" class="gravatar" src="https://secure.gravatar.com/avatar/5f9770e6c24be5645e12384e284860bf?s=50" />
<a href="/users/338193910">User 0</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/338193910">delete</a>
</li><li>
<img alt="User 24" class="gravatar" src="https://secure.gravatar.com/avatar/4595a63d7d99d2f332e58d967cbbe515?s=50" />
<a href="/users/357172992">User 24</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/357172992">delete</a>
</li><li>
<img alt="User 12" class="gravatar" src="https://secure.gravatar.com/avatar/1aea9800a38f49f4339427ee1139806c?s=50" />
<a href="/users/386199561">User 12</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/386199561">delete</a>
</li><li>
<img alt="Lana Kane" class="gravatar" src="https://secure.gravatar.com/avatar/de9a58df9617af487e8b28dbb3aa50de?s=50" />
<a href="/users/409608538">Lana Kane</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/409608538">delete</a>
</li><li>
<img alt="User 8" class="gravatar" src="https://secure.gravatar.com/avatar/57326f96e0cf2fcdce1d6fcf4743e67d?s=50" />
<a href="/users/452191684">User 8</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/452191684">delete</a>
</li><li>
<img alt="User 28" class="gravatar" src="https://secure.gravatar.com/avatar/decde8d24d5db7226cceaaa0a14bb2b9?s=50" />
<a href="/users/486296279">User 28</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/486296279">delete</a>
</li><li>
<img alt="User 13" class="gravatar" src="https://secure.gravatar.com/avatar/fcc71816a3d5e6591a796fc4602019f7?s=50" />
<a href="/users/537116830">User 13</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/537116830">delete</a>
</li><li>
<img alt="User 25" class="gravatar" src="https://secure.gravatar.com/avatar/de38a1dc7022b750c4dbf3914946565e?s=50" />
<a href="/users/575485547">User 25</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/575485547">delete</a>
</li><li>
<img alt="User 1" class="gravatar" src="https://secure.gravatar.com/avatar/622eb90d6d19960af330457aecc562a1?s=50" />
<a href="/users/590306657">User 1</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/590306657">delete</a>
</li><li>
<img alt="User 5" class="gravatar" src="https://secure.gravatar.com/avatar/a81486f15e54a5e7e02fcc9314578926?s=50" />
<a href="/users/608344442">User 5</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/608344442">delete</a>
</li><li>
<img alt="User 21" class="gravatar" src="https://secure.gravatar.com/avatar/4fff217808f6fd90877428c222cded25?s=50" />
<a href="/users/622916212">User 21</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/622916212">delete</a>
</li><li>
<img alt="User 17" class="gravatar" src="https://secure.gravatar.com/avatar/d3907b7aa9d8573328daaffb87c276ad?s=50" />
<a href="/users/661521543">User 17</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/661521543">delete</a>
</li><li>
<img alt="User 29" class="gravatar" src="https://secure.gravatar.com/avatar/1702ee830ca7e896da0cb29be3d1ed7c?s=50" />
<a href="/users/737901126">User 29</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/737901126">delete</a>
</li><li>
<img alt="Michael Example" class="gravatar" src="https://secure.gravatar.com/avatar/03ea78c0884c9ac0f73e6af7b9649e90?s=50" />
<a href="/users/762146111">Michael Example</a>
</li><li>
<img alt="User 9" class="gravatar" src="https://secure.gravatar.com/avatar/e467a49a5c504bf70a082292cfdc622a?s=50" />
<a href="/users/771020115">User 9</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/771020115">delete</a>
</li><li>
<img alt="Mallory Archer" class="gravatar" src="https://secure.gravatar.com/avatar/2fcffc6793befd1fbc6121f441d34c01?s=50" />
<a href="/users/785162917">Mallory Archer</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/785162917">delete</a>
</li><li>
<img alt="User 18" class="gravatar" src="https://secure.gravatar.com/avatar/593513ce88555bd017caf8a20f9ab042?s=50" />
<a href="/users/936450328">User 18</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/936450328">delete</a>
</li><li>
<img alt="Sterling Archer" class="gravatar" src="https://secure.gravatar.com/avatar/d08d4b6a8f375370ad3b1423dca85da8?s=50" />
<a href="/users/950961012">Sterling Archer</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/950961012">delete</a>
</li><li>
<img alt="User 10" class="gravatar" src="https://secure.gravatar.com/avatar/9f1511aadf13b07a63073efb3cf80e7a?s=50" />
<a href="/users/956993830">User 10</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/956993830">delete</a>
</li><li>
<img alt="User 2" class="gravatar" src="https://secure.gravatar.com/avatar/cdd723a3c6b4599704b8ed02339b2e80?s=50" />
<a href="/users/975572189">User 2</a>
| <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/975572189">delete</a>
</li>
</ul>
<div class="pagination"><ul class="pagination"><li class="prev previous_page disabled"><a href="#">← Previous</a></li> <li class="active"><a rel="start" href="/users?page=1">1</a></li> <li><a rel="next" href="/users?page=2">2</a></li> <li class="next next_page "><a rel="next" href="/users?page=2">Next →</a></li></ul></div>
<footer class="footer">
<small>
The <a href="http://www.railstutorial.org/">Ruby on Rails Tutorial</a>
by <a href="http://www.michaelhartl.com/">Michael Hartl</a>
</small>
<nav>
<ul>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
<li><a href="http://news.railstutorial.org/">News</a></li>
</ul>
</nav>
</footer>
</div>
</body>
</html>
Response of watching "p user" is:
#<User id: 14035331, name: "User 19", email: "user-19@example.com", created_at: "2016-01-26 04:13:50", updated_at: "2016-01-26 04:13:50", password_digest: "$2a$04$dTwCZcN7xWivGrZvlJrpzuBNZDaOKd4paCesVp8tK0k...", remember_digest: nil, admin: false>
Update:
Integration test is:
test "index as admin including pagination and delete links" do
log_in_as(@admin)
get users_path
assert_template 'users/index'
assert_select 'div.pagination'
first_page_of_users = User.paginate(page: 1)
first_page_of_users.each do |user|
byebug
assert_select 'a[href=?]', user_path(user), text: user.name
unless user == @admin
assert_select 'a[href=?]', user_path(user), text: 'delete',
method: :delete
end
assert_difference 'User.count', -1 do
delete user_path(@non_admin)
end
end
end
回答1:
As Tetiana Chupryna said, I revised my test's source and found out problem was iterating users to following part of code (wrong placement of end).
Updated version of test which works fine:
test "index as admin including pagination and delete links" do
log_in_as(@admin)
get users_path
assert_template 'users/index'
assert_select 'div.pagination'
first_page_of_users = User.paginate(page: 1)
first_page_of_users.each do |user|
assert_select 'a[href=?]', user_path(user), text: user.name
unless user == @admin
assert_select 'a[href=?]', user_path(user), text: 'delete',
method: :delete
end
end
assert_difference 'User.count', -1 do
delete user_path(@non_admin)
end
end
来源:https://stackoverflow.com/questions/35007124/got-failure-in-integration-testing-of-rails