vendredi 8 mai 2015

Debug integration test

When an integration test fails, even though the thing it's testing works with manual testing, it can be hard to work out what went wrong.

Here for example (fails):

test "posts a job with valid information" do
    get new_job_path
    assert_difference 'Job.count', 1 do
      post_via_redirect jobs_path, 
        job: { title:          "Great job",
               description:    "Please apply to our great job!",
               company:        "Fast company",
               contact_info:   "Email a cv to user@fastcompany.com",
               categroy_id:    "1" },
        employer: { email: "user@fastcompany.com" },
        location: { address_1: "21 Corsham Street",
                    address_2: "Islington",
                    postcode:  "N1 6DD" }
    end
    assert_template 'jobs/show'
    assert_not flash.empty?
  end

This same data entered into the website doesn't fail.

Infact, you can output the response by changing the assert_difference line to:

    assert_difference 'Job.count', 1, @response.body do

Here, I've use the "failure message" field of the test to print the body. Unfortunately, the response is looking very weird - it looks like it just refreshed the page. How can I get in and see what's wrong?

For reference, here's the controller and view being tested:

I'm using minitest.

Controller:

 def create
    @employer = Employer.find_or_create_by(employer_params)
    @location = @employer.locations.build(location_params)
    @job =      @employer.jobs.build(job_params)
    unless @location.save
      @job.errors.delete(:employer_id)
      @location.errors.delete(:lat)
      @location.errors.delete(:lng)
      render 'new' and return
    end

    @job.location = @location

    if @job.save
      flash[:success] = "Job posted successfully!"
      redirect_to @job
    else
      @job.errors.delete(:employer_id)
      @location.errors.delete(:lat)
      @location.errors.delete(:lng)
      render 'new'
    end 
  end

View:

<% provide(:title, 'Post a Job') %>

<h1>Post a Job</h1>

<div class="row">
  <div class="col-sm-4 col-sm-push-4">
    <%= form_for(@job) do |j| %>
      <%= render 'shared/error_messages' %>
      <%= j.label :category %>
      <%= j.collection_select :category_id, Category.all, :id, :name_titleize, { prompt: "Select vehicle" }, { class: 'form-control' } %>

      <%= j.label :title %>
      <%= j.text_field :title, class: 'form-control' %>

      <%= j.label :description %>
      <%= j.text_area :description, class: 'form-control' %>

      <%= j.label :contact_info %>
      <%= j.text_area :contact_info, class: 'form-control' %>

      <%= j.label :company %>
      <%= j.text_field :company, class: 'form-control' %>

      <%= fields_for @employer do |e| %>

        <%= e.label :email %>
        <%= e.email_field :email, class: 'form-control' %>

      <% end %>

      <%= fields_for :location, @location do |l| %>

        <%= l.label :address_1 %>
        <%= l.text_field :address_1, class: 'form-control' %>

        <%= l.label :address_2 %>
        <%= l.text_field :address_2, class: 'form-control' %>

        <%= l.label :postcode %>
        <%= l.text_field :postcode, class: 'form-control' %>

      <% end %>

      <%= j.submit "Post my job", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

Aucun commentaire:

Enregistrer un commentaire