如何将Ruby MiniTest :: Spec与Rails一起用于API集成测试? [英] How to use Ruby MiniTest::Spec with Rails for API integration tests?

查看:83
本文介绍了如何将Ruby MiniTest :: Spec与Rails一起用于API集成测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个包含Rails API的应用程序,并希望使用Ruby MiniTest :: Spec进行测试.

I'm building an app including a Rails API and want to use Ruby MiniTest::Spec to test.

设置它的好方法是什么?

What's a good way to set it up?

例如,良好的目录组织,良好的文件包含方式等等?

For example, good directory organization, good way to include files, etc.?

我正在使用《 Rails 3 In Action》一书中的指南,该书使用RSpec并在API上有很好的论述.最大的变化是选择了MiniTest :: Spec.

I'm using the guidelines in the book Rails 3 In Action which uses RSpec and has a great chapter on APIs. The big change is preferring MiniTest::Spec.

推荐答案

回答我到目前为止所发现的内容,以防对其他开发人员有帮助....

Answering with what I've found so far in case it's helpful to other developers....

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

spec/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

spec/factories/item.rb


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

app/controllers/api/base_controller.rb

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

app/controllers/api/items_controller.rb

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

config/routes.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

这篇关于如何将Ruby MiniTest :: Spec与Rails一起用于API集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆