使用控制器中的using方法测试辅助方法

时间:2017-05-14 15:15:13

标签: ruby-on-rails unit-testing testing rspec helper

module ApplicationHelper

def sortable(column, title = nil)
title ||= column
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
sort_arrow = ""
if(column == sort_column)
  if(direction == "desc")
    sort_arrow = "▼"
  elsif(direction == "asc")
    sort_arrow = "▲"
  end
end
link_to sort_arrow+title, {:sort => column, :direction => direction,
                :search_form => @search_form.attributes
}, {:class => css_class}

 end
end

以上是我想测试的方法,问题是sort_column方法在控制器上,因此我收到错误消息method does not exist。如何摆脱它或如何让Controller执行该方法?

编辑: 控制器:

class ResultsController < ApplicationController
 def sort_column
  params[:sort] != nil ? params[:sort] : 'ID'
 end
end

1 个答案:

答案 0 :(得分:0)

简单方法:使用 <html> <head> <title>Admin View</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body background=http://wallpapercave.com/wp/ItmVuT6.jpg> <!-- container with table headers / user records / edit function / approve function --> <div class="container"> <table class="table table-hover table-striped"> <center> <thead> <tr style="font-size: 24; color: white;"> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> <th>Status</th> <th colspan="1">Action</th> </tr> </thead> </center> <tbody style="background-color: #F0F8FF"> <?php foreach($users as $u) { ?> <tr style="font-size: 20;" id="row<?= $u->id?>"> <td width="5%;"> <?php echo $u->id; ?></td> <td><?php echo $u->first_name; ?></td> <td><?php echo $u->last_name; ?></td> <td><?php echo $u->email; ?></td> <td><?php echo ($u->approved) ? "Approved" : "Pending"; ?></td> <!--edit button--> <td><button type="button" data-id="<?= $u->id ?>" data-first_name="<?= $u->first_name ?>" data-last_name="<?= $u->last_name ?>" data-email="<?= $u->email ?>" data-approved="<?= $u->approved ?>" onclick="loadmodal(this)"><span class='glyphicon glyphicon-edit'></span></button></td> <!--approve button <td><a href= "/ci/index.php/myform/approve/?user_id=<?php echo $u->id;?>"><span class="glyphicon glyphicon-ok gi-15x" style='color: green'></span></a></td>--> </tr> <?php } ?> </tbody> </table> <!--modal--> <script type="text/javascript"> function loadmodal(button){ var first_name = $(button).data("first_name"); var last_name = $(button).data("last_name"); var email = $(button).data("email"); var id = $(button).data("id"); var status = $(button).data("approved"); /* pass values to modal */ $("#m_id").val(id); $("#m_first_name").val(first_name); $("#m_last_name").val(last_name); $("#m_email").val(email); $("#m_approved").val(status); $("input[name=r1]:checked").val() $("input[name=r2]:checked").val() $("#myModalLabel").text("Entry for User ID: " + id); $("#editModal").modal("show"); $("#formdata").submit(function(e){ e.preventDefault(); var data = $(this).serialize(); $.post("/ci/index.php/myform/edituser", data, function(html){ console.log(html); var response = $.parseJSON(html); console.log(response); if (response.status == 'success'){ $("#row"+id).html(response.html); $("#editModal").modal("hide"); } else { console.log('Error updating user'); $("#editModal").modal("hide"); } }); }); function approveRadio(){ if (document.getElementsByID('r1').checked) { rate_value = document.getElementsByID } var choice = document.getElementsByName('choice').value; } } </script> <div class='modal fade' id='editModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'> <div class='modal-dialog'> <div class='modal-content'> <div class='modal-header'> <h3 class="modal-title" id='myModalLabel'></h3> <button type='button' class='close' data-dismiss='modal' aria-label="Close"><span class='glyphicon glyphicon-remove'></span></button> </div> <!--modal body--> <div class='modal-body'> <form id="formdata"> <input type="hidden" id="m_id" name="id" value=""> <div class='form-group'> <label class='control-label' for='First Name'>First Name</label> <input type='text' id="m_first_name" name='first_name' class='form-control' value=""><br/> </div> <div class='form-group'> <label class='control-label' for='Last Name'>Last Name</label> <input type='text' id="m_last_name" name='last_name' class='form-control' value=""/><br/> </div> <div class='form-group'> <label class='control-label' for='Last Name'>Email</label> <input type='text' id="m_email" name='email' class='form-control' value=""/><br/> </div> <div class='form-group'> <label class='control-label' for='Approved'>Is user approved? Current status:</label><br> <input type="radio" id="r1" name="approved" value="1"> Yes <input type="radio" id="r2" name="approved" value="0"> No <!--m<input type="text" id="m_approved" name="status" class='form-control' value="">--> </div> <button type='submit' class='btn btn-success'>Update Entry</button> </form> </div> </div> </div> </div> </body> </html> 声明将sortable方法移至ResultsController

helper_method