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

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

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

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

RSpec

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

Q&A

解決済

2回答

2405閲覧

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

sanami

総合スコア11

Ruby

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

RSpec

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

0グッド

0クリップ

投稿2015/06/07 06:26

下記の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

回答2

0

ベストアンサー

cries() メソッドの定義を追加すれば テストをパスします。

lib/animal.rb

ruby

1class Animal 2 3 def name 4 @name 5 end 6 7 def name=(val) 8 @name = val 9 end 10 11 def type 12 @type 13 end 14 15 def cries=(val) 16 @cries = val 17 end 18 19 def cries 20 @cries 21 end 22end
$ rspec ... Finished in 0.00273 seconds (files took 0.13491 seconds to load) 3 examples, 0 failures

投稿2015/06/08 21:30

katoy

総合スコア22324

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

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

sanami

2015/06/11 11:44

いつもご回答ありがとうございます。 記述していただいた通りエラーなしで実行できたのですが、 下記のようなsuperclassを用いた場合はrbファイルの構成はどうしたらよろしいでしょうか? anima_spec.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--- 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--- よろしくお願い致します。
katoy

2015/06/11 14:46

$ cat lib/dog.rb class Dog < Animal def initialize @type = 'dog' @name = 'tom' @cries = 'bark' end end $ cat spec/spec_helper.rb $LOAD_PATH.unshift File.expand_path('../lib', __FILE__) require 'animal' require 'dog' $ rspec ....... Finished in 0.00466 seconds (files took 0.15541 seconds to load) 7 examples, 0 failures
sanami

2015/06/16 04:10

ご解答ありがとうございました。 無事エラーなしで実行出来ました。
guest

0

エラーに至るまでの手順が不明ですが:

mkdir workspace;cd workspace mkdir lib bundle init # エディタから「gem 'rspec'」 を追記 bundle rspec --init # 初期化(テンプレートの生成)

spec/animal_spec.rb にテスト内容を記述
lib/animal.rb にAnimal クラスとそのメソッドを記述
spec/spec_helper.rb の適当な箇所(行頭あたり)にrequire_relative '../lib/animal.rb を追記

以上でエラーなく動いてくれるはずです

詳細についてはRelish や、そのGetting started を参照してみてください

rspec の各コマンドについてはrspec --help で確認することができます

Guard(+ guard-rspec) を利用すると、ファイル内容の変更を検知して自動的にrspec を走らせてくれます

Links

edit: asciinema で手順を録画してみました

投稿2015/06/07 20:44

編集2015/06/07 21:38
gouf

総合スコア2321

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

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

sanami

2015/06/08 12:59

申し訳ありません説明不足でした。 どのようなrbファイルを通せばエラーが出ないかということなのですが、 自作で作った下記のファイルで試すと animal.rb #---start--- class Animal def name @name end def name=(val) @name = val end def type @type end def cries=(val) @cries = val end end #---end--- c:\work>rspec spec\animal_spec.rb ..F Failures: 1) Animal initialize #cries Failure/Error: subject { animal.cries } NoMethodError: undefined method `cries' for #<Animal:0x20a4dd0> # ./spec/animal_spec.rb:17:in `block (4 levels) in <top (required)>' # ./spec/animal_spec.rb:18:in `block (4 levels) in <top (required)>' Finished in 0.01302 seconds (files took 0.5398 seconds to load) 3 examples, 1 failure このようなエラーが出てしまいます。 Failed examples: rspec ./spec/animal_spec.rb:18 # Animal initialize #cries
gouf

2015/06/08 16:45

メソッド: a. `def some_method(value);end` b. `def some_method=(value);en`d と、a, bこの2つは別物です テスト側ではa のほうを呼び出し、実装側ではb のほうを書いています あべこべなので、どちらかをどちらかに合わせる必要があるでしょう getter/setter とよばれる このようなメソッドを定義していく場合、Ruby ではattr_reader, attr_writer, attr_accessor の3つが用意されているので、そちらを利用するのがいいでしょう 用例: ``` class MyClass attr_writer :some_method def another_method; end end my_class = MyClass.new my_class.some_method = 10 p my_class.some_method # => 10 ``` * [instance method Module#attr_accessor](http://docs.ruby-lang.org/ja/2.2.0/method/Module/i/attr_accessor.html) * [class Module](http://docs.ruby-lang.org/ja/2.2.0/class/Module.html#I_ATTR_WRITER) * [class Module](http://docs.ruby-lang.org/ja/2.2.0/class/Module.html#I_ATTR_READER) * [アクセスメソッド - クラスの概念 - Ruby入門](http://www.rubylife.jp/ini/class/index6.html)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問