mercredi 31 août 2016

how to test params[:view]

I want to test default view (when no view kind is given, params[:view] is empty)? How to use params[:view] in testing?

question index

<div class="well well-sm">
  <strong>Display</strong>
    <div class="btn-group">
      <%= link_to params.merge(:view => 'grid'), class: "btn btn-default btn-sm" do %>
      <%= image_tag "grid.png" ,width: "15"%> Grid <% end %>
      <%= link_to params.merge(:view => 'list'), class: "btn btn-default btn-sm"  do %> <%= image_tag "list.png" ,width: "15"%> List <% end %>
    </div>
</div><br><br><br>
<div id="entries">
  <% if params[:view] == 'grid' %>
    <%= render :partial => 'grid' %>
  <% elsif params[:view] == 'list' %>
    <%= render :partial => 'list' %>
  <%else%>
    <%= render :partial => 'list' %>
  <% end %>
</div>

class QuestionsControllerTest < ActionController::TestCase
  test "default_view" do
    u = FactoryGirl.create :question
    get :index
    assert_select params[:view].empty?
  end
end

In the terminal

 1) Error:
QuestionsControllerTest#test_default_view:
ActiveRecord::RecordInvalid: Validation failed: User can't be blank
    test/controllers/questions_controller_test.rb:11:in `block in <class:QuestionsControllerTest>'

4 runs, 6 assertions, 0 failures, 1 errors, 0 skips

Aucun commentaire:

Enregistrer un commentaire