From 19ccc03f0a7cc52356cdc12e23851d2b32ef4213 Mon Sep 17 00:00:00 2001
From: "seinan.shu"
| Book | +Body | ++ | + | + |
|---|---|---|---|---|
| <%= memo.book %> | +<%= memo.body %> | +<%= link_to 'Show', book_memo_path(@book, memo) %> | +<%= link_to 'Edit', edit_book_memo_path(@book, memo) %> | +<%= link_to 'Destroy', book_memo_path(@book, memo), confirm: 'Are you sure?', method: :delete %> | +
<%= notice %>
+ ++ Book: + <%= @memo.book %> +
+ ++ Body: + <%= @memo.body %> +
+ + +<%= link_to 'Edit', edit_book_memo_path %> | +<%= link_to 'Back', book_memos_path %> diff --git a/config/routes.rb b/config/routes.rb index f3d38ab..32492b6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ BookMemo2::Application.routes.draw do - resources :books + resources :books do + resources :memos + end # The priority is based upon order of creation: # first created -> highest priority. diff --git a/db/migrate/20120614103230_create_memos.rb b/db/migrate/20120614103230_create_memos.rb new file mode 100644 index 0000000..67feaef --- /dev/null +++ b/db/migrate/20120614103230_create_memos.rb @@ -0,0 +1,11 @@ +class CreateMemos < ActiveRecord::Migration + def change + create_table :memos do |t| + t.references :book + t.text :body + + t.timestamps + end + add_index :memos, :book_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 5fc61c2..59a7659 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120526050801) do +ActiveRecord::Schema.define(:version => 20120615004432) do create_table "books", :force => true do |t| t.string "title" @@ -21,4 +21,22 @@ t.datetime "updated_at", :null => false end + create_table "comments", :force => true do |t| + t.integer "book_id" + t.text "memo" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "comments", ["book_id"], :name => "index_comments_on_book_id" + + create_table "memos", :force => true do |t| + t.integer "book_id" + t.text "body" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "memos", ["book_id"], :name => "index_memos_on_book_id" + end diff --git a/spec/controllers/memos_controller_spec.rb b/spec/controllers/memos_controller_spec.rb new file mode 100644 index 0000000..f3781ca --- /dev/null +++ b/spec/controllers/memos_controller_spec.rb @@ -0,0 +1,164 @@ +require 'spec_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +describe MemosController do + + # This should return the minimal set of attributes required to create a valid + # Memo. As you add validations to Memo, be sure to + # update the return value of this method accordingly. + def valid_attributes + {} + end + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # MemosController. Be sure to keep this updated too. + def valid_session + {} + end + + describe "GET index" do + it "assigns all memos as @memos" do + memo = Memo.create! valid_attributes + get :index, {}, valid_session + assigns(:memos).should eq([memo]) + end + end + + describe "GET show" do + it "assigns the requested memo as @memo" do + memo = Memo.create! valid_attributes + get :show, {:id => memo.to_param}, valid_session + assigns(:memo).should eq(memo) + end + end + + describe "GET new" do + it "assigns a new memo as @memo" do + get :new, {}, valid_session + assigns(:memo).should be_a_new(Memo) + end + end + + describe "GET edit" do + it "assigns the requested memo as @memo" do + memo = Memo.create! valid_attributes + get :edit, {:id => memo.to_param}, valid_session + assigns(:memo).should eq(memo) + end + end + + describe "POST create" do + describe "with valid params" do + it "creates a new Memo" do + expect { + post :create, {:memo => valid_attributes}, valid_session + }.to change(Memo, :count).by(1) + end + + it "assigns a newly created memo as @memo" do + post :create, {:memo => valid_attributes}, valid_session + assigns(:memo).should be_a(Memo) + assigns(:memo).should be_persisted + end + + it "redirects to the created memo" do + post :create, {:memo => valid_attributes}, valid_session + response.should redirect_to(Memo.last) + end + end + + describe "with invalid params" do + it "assigns a newly created but unsaved memo as @memo" do + # Trigger the behavior that occurs when invalid params are submitted + Memo.any_instance.stub(:save).and_return(false) + post :create, {:memo => {}}, valid_session + assigns(:memo).should be_a_new(Memo) + end + + it "re-renders the 'new' template" do + # Trigger the behavior that occurs when invalid params are submitted + Memo.any_instance.stub(:save).and_return(false) + post :create, {:memo => {}}, valid_session + response.should render_template("new") + end + end + end + + describe "PUT update" do + describe "with valid params" do + it "updates the requested memo" do + memo = Memo.create! valid_attributes + # Assuming there are no other memos in the database, this + # specifies that the Memo created on the previous line + # receives the :update_attributes message with whatever params are + # submitted in the request. + Memo.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, {:id => memo.to_param, :memo => {'these' => 'params'}}, valid_session + end + + it "assigns the requested memo as @memo" do + memo = Memo.create! valid_attributes + put :update, {:id => memo.to_param, :memo => valid_attributes}, valid_session + assigns(:memo).should eq(memo) + end + + it "redirects to the memo" do + memo = Memo.create! valid_attributes + put :update, {:id => memo.to_param, :memo => valid_attributes}, valid_session + response.should redirect_to(memo) + end + end + + describe "with invalid params" do + it "assigns the memo as @memo" do + memo = Memo.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Memo.any_instance.stub(:save).and_return(false) + put :update, {:id => memo.to_param, :memo => {}}, valid_session + assigns(:memo).should eq(memo) + end + + it "re-renders the 'edit' template" do + memo = Memo.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Memo.any_instance.stub(:save).and_return(false) + put :update, {:id => memo.to_param, :memo => {}}, valid_session + response.should render_template("edit") + end + end + end + + describe "DELETE destroy" do + it "destroys the requested memo" do + memo = Memo.create! valid_attributes + expect { + delete :destroy, {:id => memo.to_param}, valid_session + }.to change(Memo, :count).by(-1) + end + + it "redirects to the memos list" do + memo = Memo.create! valid_attributes + delete :destroy, {:id => memo.to_param}, valid_session + response.should redirect_to(memos_url) + end + end + +end diff --git a/spec/factories/memos.rb b/spec/factories/memos.rb new file mode 100644 index 0000000..e8daacb --- /dev/null +++ b/spec/factories/memos.rb @@ -0,0 +1,8 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :memo do + book {Factory.create(:book)} + body "create by request spec" + end +end diff --git a/spec/helpers/memos_helper_spec.rb b/spec/helpers/memos_helper_spec.rb new file mode 100644 index 0000000..48b8f0a --- /dev/null +++ b/spec/helpers/memos_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the MemosHelper. For example: +# +# describe MemosHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe MemosHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/memo_spec.rb b/spec/models/memo_spec.rb new file mode 100644 index 0000000..b5efca7 --- /dev/null +++ b/spec/models/memo_spec.rb @@ -0,0 +1,14 @@ +# coding:utf-8 +require 'spec_helper' + +describe Memo do + describe 'validation' do + describe 'body' do + subject { Memo.new.valid? } + it 'blank and more than 100 chars will error' do + should be_false + end + end + end +end + diff --git a/spec/requests/books_spec.rb b/spec/requests/books_spec.rb index 37522b7..103e009 100644 --- a/spec/requests/books_spec.rb +++ b/spec/requests/books_spec.rb @@ -10,6 +10,17 @@ end end +=begin + describe '/books/:id' do + let!(:book){ FactoryGirl.create :book } + subject { page } + before { visit "/books/#{book.id}" } + it "タイトルが設定されていること" do + find("#book_title").value.should == book.title + end + end +=end + describe '/books/:id/edit' do let!(:book){ FactoryGirl.create :book } subject { page } diff --git a/spec/requests/memos_spec.rb b/spec/requests/memos_spec.rb new file mode 100644 index 0000000..3f3934b --- /dev/null +++ b/spec/requests/memos_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe "Memos" do +=begin + describe "GET /memos" do + it "works! (now write some real specs)" do + # Run the generator again with the --webrat flag if you want to use webrat methods/matchers + get memos_path + response.status.should be(200) + end + end +=end + + describe '/books/:book_id/memos' do + let!(:memo){ FactoryGirl.create :memo } + subject {page} + + before {visit "/books/#{memo.book.id}"} + + context 'create body' do + let(:create_body) {'create by request spec'} + before do + fill_in 'memo[body]', with: create_body + click_on 'Create Memo' + end + + it 'page not move' do + current_path.should == book_path(memo.book) + end + + it 'success to create memo' do + should have_content memo.reload.body + memo.reload.body.should == create_body + end + + end + + + end + + +end diff --git a/spec/routing/memos_routing_spec.rb b/spec/routing/memos_routing_spec.rb new file mode 100644 index 0000000..b0f68f9 --- /dev/null +++ b/spec/routing/memos_routing_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe MemosController do + describe "routing" do + + it "routes to #index" do + get("/memos").should route_to("memos#index") + end + + it "routes to #new" do + get("/memos/new").should route_to("memos#new") + end + + it "routes to #show" do + get("/memos/1").should route_to("memos#show", :id => "1") + end + + it "routes to #edit" do + get("/memos/1/edit").should route_to("memos#edit", :id => "1") + end + + it "routes to #create" do + post("/memos").should route_to("memos#create") + end + + it "routes to #update" do + put("/memos/1").should route_to("memos#update", :id => "1") + end + + it "routes to #destroy" do + delete("/memos/1").should route_to("memos#destroy", :id => "1") + end + + end +end diff --git a/spec/views/memos/edit.html.erb_spec.rb b/spec/views/memos/edit.html.erb_spec.rb new file mode 100644 index 0000000..d01a69e --- /dev/null +++ b/spec/views/memos/edit.html.erb_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe "memos/edit" do + before(:each) do + @memo = assign(:memo, stub_model(Memo, + :book => nil, + :body => "MyText" + )) + end + + it "renders the edit memo form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => memos_path(@memo), :method => "post" do + assert_select "input#memo_book", :name => "memo[book]" + assert_select "textarea#memo_body", :name => "memo[body]" + end + end +end diff --git a/spec/views/memos/index.html.erb_spec.rb b/spec/views/memos/index.html.erb_spec.rb new file mode 100644 index 0000000..ea0820f --- /dev/null +++ b/spec/views/memos/index.html.erb_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe "memos/index" do + before(:each) do + assign(:memos, [ + stub_model(Memo, + :book => nil, + :body => "MyText" + ), + stub_model(Memo, + :book => nil, + :body => "MyText" + ) + ]) + end + + it "renders a list of memos" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => nil.to_s, :count => 2 + assert_select "tr>td", :text => "MyText".to_s, :count => 2 + end +end diff --git a/spec/views/memos/new.html.erb_spec.rb b/spec/views/memos/new.html.erb_spec.rb new file mode 100644 index 0000000..fada45f --- /dev/null +++ b/spec/views/memos/new.html.erb_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe "memos/new" do + before(:each) do + assign(:memo, stub_model(Memo, + :book => nil, + :body => "MyText" + ).as_new_record) + end + + it "renders new memo form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => memos_path, :method => "post" do + assert_select "input#memo_book", :name => "memo[book]" + assert_select "textarea#memo_body", :name => "memo[body]" + end + end +end diff --git a/spec/views/memos/show.html.erb_spec.rb b/spec/views/memos/show.html.erb_spec.rb new file mode 100644 index 0000000..4bfed0d --- /dev/null +++ b/spec/views/memos/show.html.erb_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe "memos/show" do + before(:each) do + @memo = assign(:memo, stub_model(Memo, + :book => nil, + :body => "MyText" + )) + end + + it "renders attributes in" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + rendered.should match(//) + rendered.should match(/MyText/) + end +end