Search code examples
ruby-on-rails-3tddruby-on-rails-pluginscapybara

Capybara and Rails, Why my link expected to return some thing?


I am trying to test that a link dose exist on the page, I tried to check all the nesting tags that contain the link tag like that:

response.body.should have_selector("div.page_margins div.page div#nav div.hlist ul li#2")

and it passes correctly, but, if I added the link tag to the test like this:

  response.body.should have_selector("div.page_margins div.page div#nav div.hlist ul li#2 a",:text => "Next")

I get the error:

expected css "div.page_margins div.page div#nav div.hlist ul li#2 a#next_page" with text "Next" to return something

If I test it with have_link like this:

response.body.should have_link("div.page_margins div.page div#nav div.hlist ul li#2 a#next_page")

I get the error:

expected link "div.page_margins div.page div#nav div.hlist ul li#2 a#next_page" to return something

Can any body help please ? I love rails, but, I still need a hand to get along with testing ..

EDIT

Here is the page.html, I've noticed that the html in content_for in which the link is rendered is not rendered in yield

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-type">
<title></title>
<script src="/assets/application.js" type="text/javascript"></script><link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css">
<script type="text/javascript">
//<![CDATA[
var auto_log_off = false;
//]]>
</script><script type="text/javascript">
//<![CDATA[
var student_logged = false;
//]]>
</script><script src="/assets/sessions.js" type="text/javascript"></script><!-- add your meta tags here --><link href="/assets/application_yaml/css/my_layout.css" rel="stylesheet" type="text/css">
<!--[if lte IE 7]>  <![endif]--><link href="/assets/application_yaml/css/patch_my_layout.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="page_margins">
<a href="#dialog" name="modal"></a>
<div id="topnav">
<!-- start: skip link navigation -->
<a class="skip" href="#navigation" title="skip link">Skip to the navigation</a>
<span class="hideme">.</span>
<a class="skip" href="#content" title="skip link">Skip to the content</a>
<span class="hideme">.</span>
<!-- end: skip link navigation -->
</div>
<!-- start: skip link navigation -->
<!-- end: skip link navigation -->
<div class="page">
<div id="header">
<h1>Welcome to course builder!!</h1>
<p><a href="/" id="home_link">Home</a></p>
</div>
<div id="nav">
<!-- skiplink anchor: navigation -->
<a id="navigation" name="navigation"></a>
<div class="hlist">
<!-- main navigation: horizontal list -->

<div class="quiz_review_buttons">

<!--
<ul>
<li class="active"><strong>Button 1</strong></li>
<li><a href="#">Button 2</a></li>
<li><a href="#">Button 3</a></li>
</ul>
-->
</div>
<!-- <ul> -->
<!-- <li class="active"><strong>Button 1</strong></li> -->
<!-- <li><a href="#">Button 2</a></li> -->
<!-- <li><a href="#">Button 3</a></li> -->
<!-- <li><a href="#">Button 4</a></li> -->
<!-- <li><a href="#">Button 5</a></li> -->
<!-- </ul> -->
</div>
</div>
<div id="main">
<div id="col1">
<div class="clearfix" id="col1_content">
<!-- add your content here -->

<div class="debug_div">
<p>
<b>
devise/sessions#new
</b>
</p>
</div>
</div>
</div>
<div id="col3">
<div class="clearfix" id="col3_content">
<!-- add your content here -->
<div class="alert" id="notice_alert">You need to sign in or sign up before continuing.</div>
<script type="text/javascript"></script><!-- <div style="clear:both"></div> --><h2>Sign in for student</h2>

<form accept-charset="UTF-8" action="/students/sign_in" class="student_new" id="student_new" method="post">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"></div>
  <div>
<label for="student_email">Email</label><br><input id="student_email" name="student[email]" size="30" type="email" value="">
</div>

  <div>
<label for="student_password">Password</label><br><input id="student_password" name="student[password]" size="30" type="password">
</div>

    <div>
<input name="student[remember_me]" type="hidden" value="0"><input id="student_remember_me" name="student[remember_me]" type="checkbox" value="1"><label for="student_remember_me">Remember me</label>
</div>

  <div><input name="commit" type="submit" value="Sign in"></div>
</form>

  <a href="/students/sign_up">Sign up</a><br><a href="/students/password/new">Forgot your password?</a><br>
</div>
<!-- IE Column Clearing -->
<div id="ie_clearing"> </div>
</div>
</div>
<div id="footer">
Layout based on
<a href="http://www.yaml.de/">YAML</a>
</div>
</div>
</div>
</body>
</html>

Solution

  • Also, in controller specs, make sure you call render_views when checking markup. .