質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Q&A

解決済

1回答

1207閲覧

rspec実行時のrbファイルの構成

sanami

総合スコア11

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

0グッド

0クリップ

投稿2015/06/07 12:14

下記のrspecファイルをエラーなしで実行するには、
どのようなrbファイルを作成すればよろしいでしょうか?

#---start---
require 'spec_helper'

describe Animal do
context 'initialize' do
let(:animal) {Animal.new}
describe '.name' do
subject { animal.name }
it { expect(subject).to be_nil }
end
describe '.type' do
subject { animal.type }
it { expect(subject).to be_nil }
end
describe '#cries' do
subject { animal.cries }
it { expect(subject).to be_nil }
end
end
end
#---end---

spec_helper.rb
#---start---
$LOAD_PATH.unshift File.expand_path('../lib',FILE)

require 'animal'

#---end---

よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

lib/animal.rb を次のようにするとよいです。

lang

1# encoding: utf-8 2# 3class Animal 4 attr_accessor :name, :type, :cries 5end

実行結果;

$ rspec spec/animal_spec.rb ... Finished in 0.00283 seconds (files took 0.14079 seconds to load) 3 examples, 0 failures

投稿2015/06/20 00:40

katoy

総合スコア22324

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sanami

2015/06/25 02:07

いつもご回答ありがとうございます。 書いていただいた記述で実行可能となりました。 追加する形で質問なのですが、 下記のrbファイルを用いて追加する形で、rspec実行の際、 より簡潔にrbファイルを記述することは可能でしょうか? dog_spec.rb #---start--- require 'spec_helper' describe Dog do describe '#superclass' do subject { Dog.superclass } it { expect(subject).to eq Animal } end let(:dog) {Dog.new} describe ',name' do subject { dog.name } it { expect(subject).to eq 'tom' } end describe ',type' do subject { dog.type } it { expect(subject).to eq 'dog' } end describe'#cries' do subject { dog.cries } it { expect(subject).to eq 'bark' } end end #---end--- cat_spec.rb #---start--- require 'spec_helper' describe Cat do describe '#superclass' do subject { Cat.superclass } it { expect(subject).to eq Animal } end let(:dog) {Cat.new} describe ',name' do subject { dog.name } it { expect(subject).to eq 'tama' } end describe ',type' do subject { dog.type } it { expect(subject).to eq 'cat' } end describe'#cries' do subject { dog.cries } it { expect(subject).to eq 'mew' } end end #---end--- dog.rb #---start--- class Dog <Animal def initialize(name='tom' ,type='dog' ,cries='bark') @name = name @type = type @cries = cries end end #---end--- cat.rb #---start--- class Cat < Animal def initialize(name='tama' ,type='cat' ,cries='mew') @name = name @type = type @cries = cries end end #---end--- よろしくお願いします。
katoy

2015/06/25 14:43

次のようにするという案もあります。 (Dog と Cat の initialize にある一連の代入文を Animal で記述してしまう) $ cat lib/animal.rb # class Animal attr_accessor :name, :type, :cries def initialize(name = nil, type = nil, cries = nil) @name = name @type = type @cries = cries end end $ cat lib/dog.rb # class Dog < Animal def initialize(name = 'tom', type = 'dog', cries = 'bark') super(name, type, cries) end end $ cat lib/cat.rb # class Cat < Animal def initialize(name = 'tama', type = 'cat', cries = 'mew') super(name, type, cries) end end なお、このコードは rubocop でチェックして警告が出ないようにもなっています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問