这个是文档中的代码。
不依赖数据库可以快捷一些。不知道 Rails 能否做到?
# spec/web/controllers/users/show_spec.rb
require 'spec_helper'
require_relative '../../../../apps/web/controllers/users/show'
RSpec.describe Web::Controllers::Users::Show do
let(:action) { Web::Controllers::Users::Show.new(repository: repository) }
let(:user) { User.new(id: 23, name: 'Luca') }
let(:repository) { double('repository', find: user) }
it "is successful" do
response = action.call(id: user.id)
expect(response[0]).to eq 200
expect(action.user).to eq user
expect(action.exposures).to eq({user: user})
end
end
# apps/web/controllers/users/show.rb
module Web::Controllers::Users
class Show
include Web::Action
expose :user
def initialize(repository: UserRepository.new)
@repository = repository
end
def call(params)
@user = @repository.find(params[:id])
end
end
end