Skip to content

stores snippets

José Bonnet edited this page Nov 9, 2017 · 3 revisions

This is issue #1:

Snippets have a title (optional), a body (mandatory), an id (automatically created and incremented), a created_at date and an updated_at date (both automatically defined).

Create a table

$ RACK_ENV=development bundle exec rake db:create_migration NAME=add_status
db/migrate/20171107143630_add_snippet.rb

Edit the db/migrate/20171107143630_add_snippet.rb file, adding the following:

class AddSnippet < ActiveRecord::Migration[5.1]
  def change
    create_table :snippets do |t|
      t.string :title
      t.text :body
      t.timestamps
    end
  end
end

Make sure config/database.yml is set:

development: &common_settings
  adapter: sqlite3
  encoding: unicode
  database: db/snippet_development.sqlite3

test:
  <<: *common_settings
  database: db/snippet_test.sqlite3

production:
  <<: *common_settings
  database: db/snippet_production.sqlite3

Create the database by running:

$ RACK_ENV=development bundle exec rake db:create
Created database 'db/snippet_development.sqlite3'
Created database 'db/snippet_test.sqlite3'

Migrate it by doing:

$ RACK_ENV=development bundle exec rake db:migrate
== 20171107143630 AddSnippet: migrating =======================================
-- create_table(:snippets)
   -> 0.0020s
== 20171107143630 AddSnippet: migrated (0.0021s) ==============================

Verify it with

$ sqlite3 db/snippet_development.sqlite3 

Model tests

spec/spec_helper.rb:

<pre> require 'rack/test' require 'rspec'

ENV['RACK_ENV'] ||= 'test'

$: << File.expand_path('../..', FILE) require 'snippets'

def app SnippetApp end

RSpec.configure do |config| config.include Rack::Test::Methods config.mock_with :rspec end </pre>

Create the @test@ database:

<pre> $ RACK_ENV=test bundle exec rake db:migrate == 20171107143630 AddSnippet: migrating ======================================= — create_table(:snippets) → 0.0013s == 20171107143630 AddSnippet: migrated (0.0016s) ============================== </pre>

Create a model test file for @Snippet@ class (@spec/models/snippet_spec.rb@):

<pre> require_relative '../spec_helper'

RSpec.describe Snippet, type: :model do it "is valid with a title and a body" it "is invalid without a body" end </pre>

Run it by

<pre> $ bundle exec rspec spec/models/ --format documentation

Snippet is valid with a title and a body (PENDING: Not yet implemented) is invalid without a body (PENDING: Not yet implemented)

Pending: (Failures listed here are expected and do not affect your suite’s status)

1) Snippet is valid with a title and a body
   # Not yet implemented
   # ./spec/models/snippet_spec.rb:4
2) Snippet is invalid without a body
   # Not yet implemented
   # ./spec/models/snippet_spec.rb:5

Finished in 0.0019 seconds (files took 3.17 seconds to load) 2 examples, 0 failures, 2 pending </pre>

Now change @spec/models/snippet_spec.rb@ to:

<pre> require_relative '../spec_helper'

RSpec.describe Snippet, type: :model do it "is valid with a title and a body" do snippet = Snippet.new( title: "2017-11-07", body: "cwThis is today’s snippet") expect(snippet).to be_valid end it "is invalid without a body" end </pre>

Run model tests again:

<pre> $ bundle exec rspec spec/models/ --format documentation

Snippet is valid with a title and a body is invalid without a body (PENDING: Not yet implemented)

Pending: (Failures listed here are expected and do not affect your suite’s status)

1) Snippet is invalid without a body
   # Not yet implemented
   # ./spec/models/snippet_spec.rb:8

Finished in 0.04692 seconds (files took 2.49 seconds to load) 2 examples, 0 failures, 1 pending </pre>

%{color:green}Green!%

Let’s complete the model tests. Change the @app/models/snippet.rb@ file to:

<pre> require 'sinatra/activerecord'

class Snippet < ActiveRecord::Base validates :body, presence: true end </pre>

and the @spec/models/snippet_spec.r@ file to:

<pre> require_relative '../spec_helper'

RSpec.describe Snippet, type: :model do it "is valid with a title and a body" do snippet = Snippet.new( title: "2017-11-07", body: "This is today’s snippet") expect(snippet).to be_valid end it "is valid without a title and with a body" do snippet = Snippet.new( title: "", body: "This is today’s snippet") expect(snippet).to be_valid end it "is invalid without a body" do snippet = Snippet.new( title: "2017-11-07", body: "") expect(snippet).not_to be_valid end end </pre>

And run tests again:

<pre> $ bundle exec rspec spec/models/ --format documentation

Snippet is valid with a title and a body is valid without a title and with a body is invalid without a body

Finished in 0.05931 seconds (files took 3.67 seconds to load) 3 examples, 0 failures </pre>

Perfect!

Clone this wiki locally