As a course administrator,
In order to be able to issue certificates,
I want to be able to create a course offering with a description and multiple delivery dates
We are talking about multiple delivery dates for each Course, right? That is a crucial feature to implement. Each course must be held at some point, so that students can come, learn and get a certificate.
That is the next feature we will be working on.
Add the following scenario to your feature file:
# features/course_create.feature
Scenario: Add a delivery date to course
Given the course "Basic programming" is created
And I am on the Course index page
And I click on "Add Delivery date" for the "Basic programming" Course
And I fill in "Start" with "2015-12-01"
And I click "Submit" link
Then I should be on the Course index page
And I should see "Basic programming"
And I should see "Delivery dates:"
And I should see "2015-12-01"
We need to add two more step definitions:
# features/step_definitions/application_steps.rbGiven(/^the course "([^"]*)" is created$/) do|name| steps %( Given I am on the home page And I am a registered and logged in user And I click "All courses" link And I click "Create course" link And I fill in "Course Title" with "#{name}" And I fill in "Course description" with "Your first step into the world of programming" And I click "Create" link )endAnd(/^I click on "([^"]*)" for the "([^"]*)" ([^"]*)$/) do|element, name, model| object = Object.const_get(model).find(name: name).firstfind("#course-#{object.id}").click_link(element)end
In the first step we re-use some of the previously defined steps to define course creation step.
As for the second step, since we can have multiple courses displayed on the Course index page, this test will make sure that we click on the correct link to add start date to a specific course. We identify each course on the page markup using an id selector, the id is built by concatenating course- with the database id of every courses.
First thing to do is to retrieve the object from the database using the name provided to the test, in this case "Basic programming". Assuming the id of that course is 1, the second line of the step will look on the page for portion of markup with id course-1 and click on Add Delivery date link within that section.
Modify your courses/index.erb like this:
# lib/views/courses/index.erb
<% if @courses.any? %>
<% @courses.each do |course| %>
<div id="course-<%= course.id %>">
<h3><%= course.title %></h3>
<p><%= course.description %></p>
<%= link_to 'Add Delivery date', "/courses/#{course.id}/add_date" %>
</div>
<% end %>
<% else %>
<h1>You have not created any courses</h1>
<% end %>
<% if current_user %>
<%= link_to 'Create course', '/courses/create' %>
<% end %>
Wraps each course instance in a <div> with a unique id
Adds a Add Delivery date link to each course with an unique url (that will make it possible for the controller to know what course we want to add the date to)
First, run your specs to see them fail and then add the assosiations to your models:
# lib/course.rb...has n,:deliveries
# lib/delivery.rb...belongs_to :course
If we run our features now we get an error while saving the instance of Delivery we try to create. In order to make that work we need to update our form on add_date.erb and update the method we use to create the Delivery.
First we need to add a hidden field with the id ocf the course we are working with:
# lib/views/courses/add_date.erb
<% form_tag('/courses/new_date', method: 'post') do %>
<%= hidden_field_tag :course_id, value: @course.id %>
...
<% end %>
Now, let's shift our attention to the controller and the post request. Update it to: