Rspecユニットテストからではなく、以下を使用place method
して戻るようにしています:true
nil
クラス
require 'active_model'
require_relative 'board'
require_relative 'direction'
require_relative 'report'
class Actions
include ActiveModel::Validations
attr_accessor :board
def initialize
@board = Board.new
@move = Direction::Move.new
@report = Report.new
end
def place(x_coordinate, y_coordinate, direction = :north)
x_coordinate.between?(@board.left_limit, @board.right_limit) &&
y_coordinate.between?(@board.bottom_limit, @board.top_limit) &&
@move.directions.grep(direction).present?
@report.log(x_coordinate, y_coordinate, direction)
end
end
Rspec テスト
require_relative '../spec_helper'
require 'board'
require 'actions'
require 'direction'
describe Board do
let(:board) { Board.new }
let(:action) { Actions.new }
describe '#initialize' do
it { expect(board.valid?).to be_true }
it { expect(action.valid?).to be_true }
end
describe 'validations' do
it 'should not exceed top limit' do
expect(action.place(1, 6)).to be_false
end
it 'should not exceed bottom limit' do
expect(action.place(1, 0)).to be_false
end
it 'should not exceed right limit' do
expect(action.place(6, 1)).to be_false
end
it 'should not exceed left limit' do
expect(action.place(0, 1)).to be_false
end
it 'should place robot within its limits' do
expect(action.place(1, 1)).to be_true
end
it 'should not accept non-integer values' do
expect{action.place('a', 'b')}.to raise_error(ArgumentError)
end
end
describe 'actions' do
it 'place the robot on the board facing south' do
expect(action.place(1, 1, Direction::South)).to be_true
end
end
end
true 値を返す必要があるすべてのテストが失敗し、nil が返されます
検証に合格した場合に true を返す方法はありますか?