In previous post you saw first pat of the Test-Driven Development (TDD) with Rails 3 and rspec.
Today I will show you about how to write tests.
To create model run this command.
rails g model Task task:string
Here model name is singular to execute active records which will execute the command like Tasks.all
This will create a migration file.
Migrations are a convenient way for you to alter your database in a structured and organized manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run them. You’d also have to keep track of which changes need to be run against the production machines next time you deploy.
Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate. Active Record will work out which migrations should be run. It will also update your db/schema.rb file to match the structure of your database.
To see the generated migration code got to db/migrate/migration_version.rb
class CreateTasks < ActiveRecord::Migration def change create_table :tasks do |t| t.string :task t.timestamps end end end
This is our very simple migration file.
Migration didn’t create database table for you. To create the table you have to run migration command.
To create table run –
To access the Rails back run –
This is similar to irb.
Check schema: To check schema you should write the model name in the console. Then you will see the schema.
All record: To see all record you should run this commad
Create a new row: Run this command to create new row from console
Task.create :task => 'Go to the store'
Find a specific record: Run
Create a test:
Now we want to test that when we go to home page there should be list of tasks.
So, let’s modify our spec file.
require 'spec_helper' describe "Tasks" do before do @task = Task.create :task => 'go to bed' end describe "GET /tasks" do it "display some tasks" do visit tasks_path page.should have_content "go to bed" end end end
If we run –
now test show some error.
Because we didn’t put the find all task method in our controller and view.
Lets add this code to our Tasks controller.
def index @task = Task.new @tasks = Task.all end
And view –
<h1>Tasks</h1> <ul> <% for task in @tasks %> <li id="task_<%= task.id %>"> <%= task.task %> </li> <% end %> </ul>
Now we write test for create tasks.
So let’s modify our spec file.
require 'spec_helper' describe "Tasks" do before do @task = Task.create :task => 'go to bed' end describe "GET /tasks" do it "display some tasks" do visit tasks_path page.should have_content "go to bed" end it "creates a new page" do visit tasks_path fill_in 'Task', :with => 'go to work' click_button "Create Task" current_path.should == tasks_path page.should have_content 'go to work' save_and_open_page end end end
This test will fill up a form and submit button called “Create Task”
If we run this we get some error on our test. This because we didn’t create our form yet.
So, lets create our form in view and also create the create method in controller. This method process our submitted data.
To create form modify your view.
<%= form_for Task.new do |f| %> <%= f.label :task %> <%= f.text_field :task %> <%= f.submit %> <% end %>
Now add create method to controller –
def create #render :text => params.inspect Task.create params[:task] redirect_to :back end
If we run test , everything looks good.