Pregunta

Estoy construyendo una aplicación que incluye una API de rieles y desea utilizar Ruby MinItest :: especificaciones para probar.

¿Qué es una buena manera de configurarlo?

Por ejemplo, buena organización de directorios, buena manera de incluir archivos, etc.?

Estoy usando las directrices en los Rails de Libros 3 en acción que utiliza RSPEC y tiene un gran capítulo sobre API.El gran cambio es preferible minitest :: espec.

¿Fue útil?

Solución

Respondiendo con lo que he encontrado en caso de que sea útil para otros desarrolladores ...

Especificación / API / ITEMS_SPEC.RB

require 'spec_helper'

class ItemsSpec < ActionDispatch::IntegrationTest

  before do
    @item = Factory.create(:item)
  end

  describe "items that are viewable by this user" do
    it "responds with good json" do
      get "/api/items.json"
      response.success?.must_equal true
      body.must_equal Item.all.to_json
      items = JSON.parse(response.body)
      items.any?{|x| x["name"] == @item.name}.must_equal true
    end
  end

end

SPEP / SPEC_HELPER.RB

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
gem 'minitest'
require 'minitest/autorun'
require 'action_controller/test_case'
require 'capybara/rails'
require 'rails/test_help'
require 'miniskirt'
require 'factories'
require 'mocha'

# Support files                                                                                                                                                                                                                                                                  
Dir["#{File.expand_path(File.dirname(__FILE__))}/support/*.rb"].each do |file|
  require file
end

Especificación / Fábricas / Item.RB

Factory.define :item do |x|
  x.name { "Foo" }
end

APP / CONTROLADORES / API / BASE_CONTROLER.RB

class Api::BaseController < ActionController::Base
  respond_to :json
end

APLICACIÓN / CONTROLADORES / API / ITEMS_CONTROLER.RB

class Api::ItemsController < Api::BaseController
  def index
    respond_with(Item.all)
  end
end

CONFIG / RUTES.RB

MyApp::Application.routes.draw do
  namespace :api do
    resources :items
  end
end

GEMFILE

group :development, :test do
  gem 'capybara'  # Integration test tool to simulate a user on a website.
  gem 'capybara_minitest_spec'  # MiniTest::Spec expectations for Capybara node matchers.
  gem 'mocha'  # Mocking and stubbing library for test doubles for Ruby.
  gem 'minitest', '>= 3'  # Ruby's core TDD, BDD, mocking, and benchmarking.
  gem 'minitest-capybara'  #  Add Capybara driver switching parameters to minitest/spec.
  gem 'minitest-matchers'  # RSpec/Shoulda-style matchers for minitest.
  gem 'minitest-metadata'  # Annotate tests with metadata key-value pairs.
  gem 'minitest-spec-rails'  # Drop in MiniTest::Spec support for Rails 3.
  gem 'miniskirt'  # Factory creators to go with minitest.
  gem 'ruby-prof'  # Fast code profiler for Ruby with native C code.
end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top