iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 18
0
Modern Web

RRRR的世界 (Ruby on Rails + React + Redux)系列 第 18

Day 18, Reading List - Rails部分-3

  • 分享至 

  • xImage
  •  

踩了好多雷 才有今天這篇。
以前都不寫TDD,這次剛好有機會雷死自己

這邊再講TDD,是我前面沒講的 前面沒講的 前面沒講的!!

好 前置作業:

先建立 json help

#spec/support/request_helpers.rb
module Requests
  module JsonHelpers
    def json
      JSON.parse(response.body)
    end
  end
end

spec_helper.rb 新增

Dir[File.dirname(__FILE__) + "/support/*.rb"].each {|f| require f } #新增這行
RSpec.configure do |config|
  #...
  config.include Requests::JsonHelpers, type: :controller #新增這行
end

來兩個FactoryGirl

#spec/factories/authors.rb
FactoryGirl.define do
  factory :author do
    name "Alex"
  end
end

#spec/factories/books.rb
FactoryGirl.define do
  factory :book do
    name    'aaaa'
    status  0
    author
  end
end

好,最後是rspec controller

require 'rails_helper'
RSpec.describe BooksController, type: :controller do
  render_views
  describe "GET 'index' " do
    it "returns a successful 200 response" do
      get :index, format: :json 
      expect(response).to be_success
    end

    it "returns all the books" do
      get :index, format: :json
      expect(json.length).to eq(3)
    end
  end
  describe "GET 'show' " do
    before(:each) do
      @id = 1
    end
    context "Check return status" do
      it "returns a successful 200 response" do
        get :show, params:{:id => @id}, format: :json
        expect(response).to be_success
      end
    end
    context "Check return data" do
      it "returns '/books/1.json'" do
        get :show, params:{:id => @id}, format: :json
        expect(json["id"]).to eq(1)
      end
      it "returns '/books/1.json'" do
        get :show, params:{:id => @id}, format: :json
        expect(json["name"]).to eq(Book.find(1).name)
      end
    end
  end
  describe "POST 'create' " do
    let(:author_attributes) { FactoryGirl.attributes_for(:author) }
    let(:book_attributes) { FactoryGirl.attributes_for(:book, author_attributes: author_attributes) }
    before(:each) do
      request.headers['Content-Type'] = 'application/json'
      process :create, method: :post, params: { book: book_attributes, format: :json }
    end
    context "Check return status" do
      it "returns name equal" do
        expect(json["name"]).to eq(book_attributes[:name])
      end
    end
  end
  describe "PUT 'books/:id' " do
    let(:book) { Book.first}
    before(:each) do
      request.headers['ACCEPT'] = 'application/json'
      request.headers['Content-Type'] = 'application/json'
    end
    context "Check Update all" do
      it "returns author name equal" do
        params = {"name"=>"aaaa", "status"=>2, :author_attributes=>{:name=>"Alex"}}
        process :update, method: :put, params: {:id => book.id,  book: params}, format: :json
        expect(json["name"]).to eq(params["name"])
        expect(json["status"]).to eq("finished")
        expect(json["author"]["name"]).to eq("Alex")
      end
    end

    describe "Delete 'books/:id' " do
      let(:book) { Book.first}
      before(:each) do
        request.headers['Content-Type'] = 'application/json'
      end
      context "Delete Book" do
        it "returns author name equal" do
          process :destroy, method: :delete, params: {:id => book.id}, format: :json
          expect(response.body).to eq("")
        end
      end
    end
  end
end

坑紀錄

  1. routes.rb error
  # resources :books, :default => {:format => :json}
  # ===================default忘了加s
  # 導致 get index 一直過不來 "no route matches error"
  get :index, format: :json 
  1. render_views 沒加,json沒有回來
  2. params中文問題(還沒解決 先用英文帶過)
    context "Check Update all" do
      it "returns author name equal" do
        #打中文就會死給我看...JSON Parse error
        params = {"name"=>"aaaa", "status"=>2, :author_attributes=>{:name=>"Alex"}}
        process :update, method: :put, params: {:id => book.id,  book: params}, format: :json
        expect(json["name"]).to eq(params["name"])
        expect(json["status"]).to eq("finished")
        expect(json["author"]["name"]).to eq("Alex")
      end
    end

測試寫完,明天就來寫react了(終於......


上一篇
Day 17, Reading List - Rails部分-2
下一篇
Day 19, Reading List - React部分-1
系列文
RRRR的世界 (Ruby on Rails + React + Redux)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言