RSpec 3ã®éè¦ãªå¤æ´
Myron Marston » Notable Changes in RSpec 3ã®éãªè¨³ã§ãã
誤訳ã»éããã訳ãããã¾ããããTwitterã§@nilp_ã¾ã§ãé£çµ¡é ããã¨å©ããã¾ãã
RSpec 3.0.0 RC1ã2æ¥åã«ãªãªã¼ã¹ããã¾ãããããã¦æçµçãª3.0.0ã®ãªãªã¼ã¹ãç®åã«è¿«ã£ã¦ãã¾ãã æã ã¯Î²çããã6ã¶æã«ããã使ã£ã¦ãã¾ãããæã ã¯ããããçããã¨å ±æã§ãããã¨ã«ãããããã¦ãã¾ãã
ãããæ°ããã¨ãã ã:
ãã¹ã¦ã®gemãã¡ã«ããã£ã¦
Ruby 1.8.6ã¨1.9.1ã®ãµãã¼ãããªããªãã¾ãã
ãããã®ãã¼ã¸ã§ã³ã®Rubyã¯ããªãåã«å¯¿å½ãè¿ãã¾ãããRSpecã¯ãããããµãã¼ããã¾ããã
Ruby 2.xã®ãµãã¼ãåä¸
æè¿ã®RSpec 2.xã®ãªãªã¼ã¹(ããªãã¡2.0ããªãªã¼ã¹ããããã¨åºããã¤)ã¯Ruby 2ãå ¬å¼ã«ãµãã¼ããã¦ãã¾ãããããRSpec 3ã§ã®ãµãã¼ãã¯ããåä¸ãã¾ããã ç¾å¨æã ã¯Ruby 2ã®æ°æ©è½ã®ããã®ãµãã¼ããæä¾ãã¦ãã¾ãããã¨ãã°ãã¼ã¯ã¼ãå¼æ°ãprependãããã¢ã¸ã¥ã¼ã«ãªã©ã§ãã
æ°gem rspec-support
rspec-supportã¯ãæã ã1ã¤ä»¥ä¸ã®rspec-(core|expectations|mocks|rails)ã§å¿ è¦ã¨ãã¦ããå ±éããã³ã¼ãã®ããã«ä½¿ç¨ãã¦ããæ°ããgemã§ãã ããã¯ä»ã®ã¨ããã¨ã³ãã¦ã¼ã¶ã¼ãããã¯æ¡å¼µã©ã¤ãã©ãªã®ä½è ã使ãããããªpublicãªAPIã¯å«ã¾ãã¦ãã¾ããããããæã ã¯å°æ¥ããã¤ãã®APIãpublicã«ããããããã¾ããã ããããªããGemfileã§GitHubãã½ã¼ã¹ã«æå®ãã¦ãææ°çRSpecãå®è¡ãã¦ãããªããããªãã¯rspec-supportã®ããã«åãæå®ãããå¿ è¦ãããã¾ãã
å ç¢ã§ããããã¹ããããã¢ããã°ã¬ã¼ãããã»ã¹
RSpec 3ã«ãããå ¨ã¦ã®ç ´å£çãªå¤æ´ã¯2.99ã§ã®deprecation warningã«ç¸å½ãã¾ãã βã®éãæã ã¯ã¢ããã°ã¬ã¼ããåºæ¥ãéãã¹ã ã¼ãºã«é²ãããå¤ãã®ã¢ããã°ã¬ã¼ããè¡ãã¾ããã æã ã¯æ®µéçã«ã¢ããã°ã¬ã¼ãããããã®èª¬æã1ã¤ã«ã¾ã¨ãã¦ããã¾ãã
ã¢ããã°ã¬ã¼ãã®ããã»ã¹ã¯RSpecã®ãããè¨å®ã®æè»æ§ãé«ããæ°ããdeprecationã·ã¹ãã ã«ãç®ãåããã¾ã(deprecationããã¡ã¤ã«ã«åºåããããå ¨ã¦ã®deprecationãã¨ã©ã¼ã«ã§ãã)ã ããã¦ãããã¯éè¤ããdeprecationã®åºåãå°ãªãããããã«è¨è¨ããã¦ãã¾ãã
æ¹åãããããã¥ã¡ã³ã
æã ã¯å ¨ã¦ã®gemã®APIããã¥ã¡ã³ããã¢ãããã¼ãããããã¯ã½é å¼µãã¾ããã ãããã¯ç¾å¨rubydoc.infoã§ãã¹ãããã¦ãã¾ã:
...ããããç¾å¨ããããã¯ããããã»ã«ããã¹ãããããã«ãrspec.infoã®æ´æ°ãè¡ã£ã¦ãã¾ãã
ããã¥ã¡ã³ãã¯ä»ãä½æ¥ä¸ã§ã(å®ã®ã¨ããã¤ãä½æ¥ä¸)ã
æã ã¯SemVerã®ä¸ç°ã¨ãã¦å ¨ã¦ã®ãããªãã¯APIãæ確ã«å ¬è¡¨ã§ããããã«ãã¾ããã æã ã¯å ¨ã¦ã®3.xã®ãªãªã¼ã¹ã®éãå ¨ã¦ã®ãããªãã¯APIãã¡ã³ããã³ã¹ããããã«å ¨èº«å ¨éãå°½ããã¦ãã¾ãã ä¸æ¹ã§ããã©ã¤ãã¼ãAPIã«ã¤ãã¦ã¯ã3.xã®ãªãªã¼ã¹ä¸ã®ã©ããã§ããããå¤æ´ã§ãããããªæè»æ§ã確ä¿ãããã£ãã®ã§ãã©ã¤ãã¼ãã¨ãªã£ã¦ãã¾ãã
ã©ãããæã ããã©ã¤ãã¼ãã¨å®£è¨ããAPIã使ããªãã§ãã ããã ããããªããæ¢åã®public APIã«ãã£ã¦åãä¸ãããã¦ããªããã¼ãºãè¦ã¤ããããã©ããå°ãã¦ãã ããã æã ã¯ãªãã®ãã¼ãºã®ããåãã§ãã©ã¤ãã¼ãAPIãpublicã«ããããæ°ããããªãã®ã¦ã¼ã¹ã±ã¼ã¹ã«åã£ãAPIãã¤ããã§ãããã
Gemã«ç½²åãã¤ãããªã
æã ã¯ç§éã®gemã«ç½²åãã¤ãã¯ããã¾ããã ãã°ããã¯gemã®ç½²åã·ã¹ãã ã¯çæ³ã¨ã¯ç¨é ãã§ããã ããããã½ãªã¥ã¼ã·ã§ã³ãéçºããã¯ããã¦ãã¾ãã ããããªãããã¯ãã·ã§ãã æã ã¯ç§éã®å ¬ééµãGitHubã«ç½®ãã¦ãã¾ãã
ç¾å¨ã®gemç½²åã·ã¹ãã ã«ã¤ãã¦ãã詳ãããã¨ãç¥ããããã°ãA Practical Guide to Using Signed Ruby Gemsãè¦ã¦ãã ããã
ã¼ãã¢ã³ãã¼ãããã¢ã¼ã
RSpecã¯ä»ãããªãã¢ã³ãã¼ãããããªãã§ä½¿ããã¨ãã§ãã¾ãã
ãã®ããã®å¤§é¨åã®åå°ãæ°ããexpect
ãã¼ã¹ã®è¨æ³ãrspec-expectationsã¨rspec-mocksã«è¿½å ããæè¿ã®2.xã®ãªãªã¼ã¹ã§ä½ããã¾ããã
æã
ã¯RSpec 3ã¸ã®éã®ãã§ãæ®ãã®é¨åãã¨ãã®ãããæ®ã£ã¦ããã¢ã³ãã¼ãããã«ã¤ãã¦ã代æ¿ãæä¾ãã¾ããã
便å©ãªãã¨ã«ããªãã¯å ¨ã¦ã®ã¢ã³ãã¼ãããã1ã¤ã®ãªãã·ã§ã³ã§ç¡å¹ã«ã§ãã¾ã:
spec/spec_helper.rb
RSpec.configure do |c| c.disable_monkey_patching! end
ãã®è¨å®ãªãã·ã§ã³ãå®è£ ãã¦ããã¦ãããã¨ãAlexey Fedorovã
ãã詳ããæ å ±:
rspec-core
ããã¯ã¹ã³ã¼ãã®ããã®æ°ããåå: :example
㨠:context
RSpec 2.x ã¯3ã¤ã®ç°ãªãããã¯ã¹ã³ã¼ãããã£ã¦ãã¾ãã:
my_class_spec.rb
describe MyClass do before(:each) { } # ãã®ã°ã«ã¼ãã®ããããã®exampleã®åã«å®è¡ããã before(:all) { } # ãã®ã°ã«ã¼ãã®æåã®exampleã®åã«1度ã ãå®è¡ããã end
spec/spec_helper.rb
RSpec.configure do |c| c.before(:each) { } # å ¨ã¦ã®ãã¹ãã¹ã¤ã¼ãä¸ã®ããããã®exampleã®åã«å®è¡ããã c.before(:all) { } # ããããã®ãããã¬ãã«ã®ã°ã«ã¼ãã®æåã®exampleã®åã«å®è¡ããã c.before(:suite) { } # å ¨ã¦ã®specãã¡ã¤ã«ããã¼ãããããã¨ãæåã®specãå®è¡ãããåã«ä¸åº¦ã ãå®è¡ããã end
ã¨ãã©ããã¦ã¼ã¶ã¼ã¯:each
vs :all
ãä½ãæå³ãããæ··ä¹±ãããã¨ã¨ãç¹ã«:all
ãconfigãããã¯ä¸ã§ä½¿ã£ãã¨ãã«æ··ä¹±ãããã¨ãè¿°ã¹ã¦ãã¾ãã:
spec/spec_helper.rb
RSpec.configure do |c| c.before(:all) { } end
ãã®contextã§ã¯:all
ã¨ããè¨èã¯ãã¹ãã¹ã¤ã¼ãä¸ã®å
¨ã¦ã®exampleã®åã«ä¸åº¦ã®ã¿å®è¡ããããã¨ãæ¨æ¸¬ãã¦ãã¾ãã¾ããããããã¯:suite
ã®ãã¨ã§ãã
RSpec 3ã§ã¯ã:each
ã¨:all
ã¯ã¹ã³ã¼ããããæ確ã«ããã¨ã¤ãªã¢ã¹ãæã£ã¦ãã¾ã:
:example
ã¯:each
ã®ã¨ã¤ãªã¢ã¹ã§:context
ã¯:all
ã®ã¨ã¤ãªã¢ã¹ã§ãã
:each
ã¨:all
ã¯deperecatedã§ã¯ãªãæã
ãããããè¨ç»ããªããã¨ã«æ³¨æãã¦ãã ããã
ãããå®è£ ãã¦ããã¦ãããã¨ãJohn Feminellaã
ãã詳ããæ å ±:
DSLã¡ã½ãããexampleãå¼æ°ã¨ãã¦æ¸¡ã
RSpec::Core::Example
ã¯exampleã«é¢ããå
¨ã¦ã®è©³ç´°ã«ã¢ã¯ã»ã¹ããæ段ãæä¾ãã¾ã: ãã®èª¬æãå ´æãã¡ã¿ãã¼ã¿ãå®è¡çµæããªã©ãªã©ã
RSpec 2.xã§ã¯exampleã¯example
ã¡ã½ããã¨ãã¦ã©ããªããã¯ãåå¥ã®exampleããã§ãã¢ã¯ã»ã¹åºæ¥ãããã«ããããã¦ãã¾ãã:
my_class_spec.rb
describe MyClass do before(:each) { puts example.metadata } end
RSpec 3ã§ã¯ãæã
ã¯example
ã¡ã½ãããåãé¤ãã¾ããã
ãã®å¤ããã«ãinstanceã¯å
¨ã¦ã®exampleã¹ã³ã¼ãã®DSLã¡ã½ããã«ã¯ã£ããã¨å¼æ°ã¨ãã¦æ¸¡ãããããã«ãªãã¾ããã
my_class_spec.rb
describe MyClass do before(:example) { |ex| puts ex.metadata } let(:example_description) { |ex| ex.description } it 'exampleã«ã¢ã¯ã»ã¹' do |ex| # exã使ã end end
ãããã¢ã¤ãã¢ã¨å ±ã«å®è£ ãã¦ããã¦ãããã¨ãDavid Chelimsky!
ãã詳ããæ å ±:
rspec-coreã®ã¢ã³ãã¼ãããã³ã°ãç¡å¹ã«ããããã®æ°è¨å®ãªãã·ã§ã³expose_dsl_globally
RSpec 2.xã¯ãããã¬ãã«ã¡ã½ããã®describe
ããshared_examples_for
ãshared_context
ãæä¾ããããmain
ã¨Module
ãã¢ã³ãã¼ããããã¦ãã¾ããã
my_gem_spec.rb
shared_examples_for "something" do end module MyGem describe SomeClass do it_behaves_like "something" end end
RSpec 3ã§ã¯ããããã®ã¡ã½ããã¯RSpec
ã¢ã¸ã¥ã¼ã«ä¸ã§å©ç¨åºæ¥ãããã«ãªãã¾ãã(ããã«å ãã¦ã¢ã³ãã¼ãããã¨ãã¦ãå©ç¨å¯è½ã§ã)ã:
RSpec.shared_examples_for "something" do end module MyGem RSpec.describe SomeClass do it_behaves_like "something" end end
æ°ããexpose_dsl_globally
ã®è¨å®ãªãã·ã§ã³ãfalse
ã«è¨å®ãããã¨ã«ãã£ã¦ãããªãã¯rspec-coreã®ã¢ã³ãã¼ããããå®å
¨ã«åãé¤ããã¨ãã§ãã¾ã(ããã«ããä¸ã§åºã¦ããæåã®exampleã¯NoMethodError
ã®ä¾å¤ãæããããã«ãªãã¾ã)ã
spec/spec_helper.rb
RSpec.configure do |config| config.expose_dsl_globally = false end
ãããå®è£ ãã¦ããã¦ãããã¨ãJon Roweã
ãã詳ããæ å ±:
alias_example_group_to
ã§example groupã«ã¨ã¤ãªã¢ã¹ãå®ç¾©ã§ãã¾ã
RSpec 2.xã§ã¯ãæã
ã¯ããªããã¡ã¿ãã¼ã¿ä»ãã®example
ã®ã¨ã¤ãªã¢ã¹ãå®ç¾©ã§ããAPIãæä¾ãã¾ããã
ä¾ãã°ãããã¯:forcus => true
ã®ã¡ã¿ãã¼ã¿ãä¼´ã£ãit
ã®ã¨ã¤ãªã¢ã¹ã®fit
ãå®ç¾©ããããã«å
é¨ã§ä½¿ããã¦ãã¾ãã:
spec/spec_helper.rb
RSpec.configure do |config| config.alias_example_to :fit, :focus => true end
RSpec 3ã§ã¯ãæã ã¯example groupsã§ããã®æ©è½ãå©ç¨åºæ¥ãããæ¡å¼µãã¾ãã:
spec/spec_helper.rb
RSpec.configure do |config| config.alias_example_group_to :describe_model, :type => :model end
ããªãã¯ãã®ä¾ãrspec-rails
ã使ã£ã¦ããããã¸ã§ã¯ãã§å©ç¨ããdescribe User, :type => :model
ã®ãããã«describe_model User
ã使ãã¾ãã
ãããå®è£ ãã¦ããã¦ãããã¨ãMichi Huber
ãã詳ããæ å ±: - Documentation - rspec-core #493 - original discussion
example groupã®æ°ããã¨ã¤ãªã¢ã¹: xdescribe
, xcontext
, fdescribe
, fcontext
example groupã®ã¨ã¤ãªã¢ã¹ãå®ç¾©ããããã®APIãå ããã ãã§ã¯ãªããæã
ã¯åæ§ã«ããã¤ãã®è¿½å ã®ãã«ãã¤ã³ã¨ã¤ãªã¢ã¹ãå ãã¾ãã(describe
ã¨context
ä¸ã«):
xdescribe
/xcontext
ããã¨ãã°xit
ã®ãããªãã®ã§ããä¸æçã«1ã¤ã®exampleã°ã«ã¼ããã¹ãããããããã«ä½¿ãã¾ããfdescribe
/fcontext
ããã¨ãã°fit
ã®ãããªãã®ã§ããä¸æçã«1ã¤ã®exampleã°ã«ã¼ãã«:focus => true
ã®ã¡ã¿ãã¼ã¿ãå ããããã«ä½¿ãã¾ããããªãã¯config.filter_run :focus
ã«ãã£ã¦é¢å¿ãæã£ãexamplesã¨groupsãç°¡åã«ãã£ã«ã¿ã¼ã§ãã¾ãã
ãã詳ããæ å ±:
pending
ã®æå³ã®å¤æ´(ããã¦skip
ã®ç´¹ä»)
Pending exampleã¯ä»ãã»ãã¨ã¯éã£ã¦ããã®ãããã§ãã¯ããããã«å®è¡ããã¾ãã ããpendingãããã¯ã失æããããããã¯åã¨åãããã«pendingã¨ãã¦è¨é²ããã¾ãã ä¸æ¹ããããããæåããå ´åãããã¯å¤±æãããã¨ã«ãªãã¾ãã ããã¯exampleããä¿çã«ãããã¨ã¨ããããã説æãã¦ãããã¨ãå®è£ ãããã¨ãä¿çãè¿ éã«è§£æ±ºãããã¨ã確å®ã«ããæå©ãããã¾ãã
"決ãã¦å®è¡ããªã"å¤ãåä½ããµãã¼ãããããã«ãskip
ã¡ã½ããã¨ã¡ã¿ãã¼ã¿ãå ãããã¾ããã
次ã®exampleãã¯ã©ããå®è¡ããããã¨ã¯ããã¾ãã:
post_spec.rb
describe Post do skip 'not implemented yet' do end it 'does something', :skip => true do end it 'does something', :skip => 'reason explanation' do end it 'does something else' do skip end it 'does something else' do skip 'reason explanation' end end
ãã®å¤æ´ã«ãã£ã¦ãããexampleä¸ã§pendingã«ãããã¯ãããããã¨ã¯ç´å¾ãããªãåä½ã«ãªãã¾ããããªã®ã§ãã®åä½ã¯åãé¤ããã¾ããã
ãããå®è£ ãã¦ããã¦ãããã¨ãXavier Shayã
ãã詳ããæ å ±:
ã¯ã³ã©ã¤ãã¼ã®ããã®æ°ããAPI: is_expected
RSpecã¯ã¯ã³ã©ã¤ãã¼ã®ããã®è¨æ³ãé·ãéæã£ã¦ãã¾ãã:
post_spec.rb
describe Post do it { should allow_mass_assignment_of(:title) } end
ãã®contextã§ã¯ãshould
ã¯ã¢ã³ãã¼ãããããã¦ãã¾ãããshould
ã¯:expect
è¨æ³ã®ã¿ããµãã¼ãããrspec-expectationsã®è¨å®ã«ãã£ã¦åãé¤ããã¨ãåºæ¥ã¾ãã
ãã®å ´åã¯should
ã使ããããã«Object
ãã¢ã³ãã¼ãããããã®ã«å«ãªæ°ã¯ãã¾ãããããã¯ããªãã®è¨æ³ã®è¨å®ã«ããããã常ã«å©ç¨å¯è½ã§ãã
ä½åãã®ã¦ã¼ã¶ã¼ã¯ããããã使ãç¶ãããã¨ãåºæ¥ããªãããã®should
ã¨expect
è¨æ³ãã©ãé¢é£ã¥ããã®ãã«ã¤ãã¦æ¸æããã¿ãã¾ããã
ããã¯RSpec 3ã§ãå©ç¨å¯è½ãªã¾ã¾ã§ã(ããä¸åº¦ããã¾ããããªãã®è¨æ³ã®è¨å®ã«é¢ä¿ãªã)ããããæã
ã¯åæ§ã«expect
è¨æ³ã«çç¾ããªã代æ¿ã®APIã追å ãã¾ãã:
post_spec.rb
describe Post do it { is_expected.to allow_mass_assignment_of(:title) } end
is_expected
ã¯expect(subject)
ã¨ãã¦é常ã«ç°¡åã«å®ç¾©ããã¦ãããshould_not
ã¨åæ§ã«å¦å®ããæ¤è¨¼ãis_expected.not_to
ãããã£ã¼ã«ãããµãã¼ããã¾ãã
ãã詳ããæ å ±:
Exampleã°ã«ã¼ãã®å®è¡é ãåå¥ã«è¨å®ã§ãã
RSpec 2.8ããrandomãªå®è¡é ãRSpecã«åãå ¥ãã¾ãããããã¯ããªãã®specã¹ã¤ã¼ãä¸ã«ããæ æã§ã¯ãªãå®è¡é ä¾åãæããã«ããã®ã«é常ã«å½¹ã«ç«ã¤ã§ãããã RSpec 3ã§ã¯ãåã«ãªã¼ã«ãªã¢ããã·ã³ã°ã®æ©è½ã§ã¯ãªããªãã¾ããã
ããªãã¯exampleã°ã«ã¼ããã¨ã«åã£ãã¡ã¿ãã¼ã¿ã§ã¿ã°ä»ãããåå¥ã«å®è¡é åºãã³ã³ããã¼ã«ãããã¨ãã§ãã¾ãã
my_class_spec.rb
describe MyClass, :order => :defined do # å®è¡é ã®è¨å®ã«ãããããã # ãã®ã°ã«ã¼ãä¸ã®exampleãã¯å¸¸ã«å®ç¾©ãããé åºã§å®è¡ããã¾ãã end describe MyClass, :order => :random do # å®è¡é ã®è¨å®ã«ãããããã # ãã®ã°ã«ã¼ãä¸ã®exampleãã¯å¸¸ã«ã©ã³ãã ãªé åºã§å®è¡ããã¾ãã end
ããã¯ç¹ã«å®è¡é ãå®ç¾©é ããã©ã³ãã ãªé åºã«åãæ¿ããã®ã«ããã ã¡ã¾ãã ããªãã¯åé¡ãä¸åº¦ã«ãã¹ã¦è§£æ±ºãããã¨ãããç¹å®ã®ã°ã«ã¼ãã«å¯¾ãã¦ãã®æ©è½ãç¨ãã¦ãããã¨ã§ãå®è¡é åºã®ä¾åã1ã¤ãã¤è§£æ±ºãã¦ãããã¨ãå¯è½ã§ãã
ãã®ä¸ç°ã¨ãã¦ãæã
ã¯åæ§ã«--order default
ã®ååã--order defined
ã«å¤æ´ãã¾ããããªããªãæã
㯠"default" ãé常ã«å¤å²ã«æ¸¡ãæå³ãæã¤ã¨å®æããããã§ãã
ãã®æ©è½ãå®è£ ããã®ãå©ãã¦ããã¦ãããã¨ãAndy Lindemanã¨Sam Phippenã
ãã詳ããæ å ±: - Documentation
æ°ãããªã¼ãã¼ãªã³ã°ã¹ãã©ãã¸ã¼API
RSpec 3ã§ã¯ãæã
ã¯ãªã¼ãã¼ãªã³ã°ã¹ãã©ãã¸ã¼APIããªã¼ãã¼ãã¼ã«ãã¾ããã
ä¾ç¶ã¯
3ã¤ã®ã
ç°ãªãã
ã¡ã½ãã
ã ã£ãã®ã1ã¤ã®ã¡ã½ããã«ãªãã¾ãã: register_ordering
ã§ãã
ãªã¼ãã¼ãªã³ã°ã¹ãã©ãã¸ã¼ã«ååãã¤ããããã«ããã使ã£ã¦ãã ããã
spec/spec_helper.rb
RSpec.configure do |config| config.register_ordering(:description_length) do |list| list.sort_by { |item| item.description.length } end end
my_class_spec.rb
describe MyClass, :order => :description_length do # ... end
ãããã¯ããªãã¯ãããã°ãã¼ãã«ãªãªã¼ãã¼ãªã³ã°ã¨ãã¦ä½¿ããã¨ãã§ãã¾ã:
spec/spec_helper.rb
RSpec.configure do |config| config.register_ordering(:global) do |list| # ã¢ã«ãã¡ãããé ã«ã½ã¼ããã list.sort_by { |item| item.description } end end
:global
ã®ãªã¼ãã¼ãªã³ã°ã¯ãããã¬ãã«ã®example groupsã¨ã:order
ã®ã¡ã¿ãã¼ã¿ããããªãå
¨ã¦ã®example groupsãé çªä»ããããã«ä½¿ããã¾ãã
ãã詳ããæ å ±:
rspec --init
ã®ã«ã¤ã¼ã³
rspec
ã³ãã³ãã¯é·ãéãããã¸ã§ã¯ãã¹ã±ã«ãã³ãã»ããã¢ããããããã«--init
ãªãã·ã§ã³ãæä¾ãã¦ãã¾ããã
RSpec 3ã§ã¯ããã®ã³ãã³ããä½æãããã¡ã¤ã«ããç®±ããåºãã¦ãã®ã¾ã¾ä½¿ãããããããã®ãæä¾ãããããããªãã«ã¤ã¼ã³ããã¾ãããããã¦æ¨å¥¨ããè¨å®ã¨ä¸ç·ã«spec/spec_helper.rb
ãã¡ã¤ã«ãæä¾ãã¾ãã
ããæ¨å¥¨ããè¨å®ã®ä¸ã§ããã©ã«ãã«ãªãäºå®ããªããã®ã¯çæããããã¡ã¤ã«ä¸ã§ã³ã¡ã³ãã¢ã¦ãããã¦ãã¾ãã ãªã®ã§ããã¡ã¤ã«ãéãã¦ãããªãããããªãã¨æããªã¹ã¹ã¡è¨å®ãåãå ¥ããã¨ããã§ããã
ãã詳ããæ å ±:
æ°ããã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ --dry-run
ãã®ãªãã·ã§ã³ã¯ãããªãexampleãhookãå®è¡ãããããªãã®specã¹ã¤ã¼ããformatterã使ã£ã¦åºåããçµæãç»é¢ã«è¡¨ç¤ºãã¾ãã ããã¯ããã¹ããéã/è½ã¡ããæ°ã«ãããããã¹ããå®è¡ãããã®ãã¾ã£ããããã«ãããªãã®ãã¹ãã¹ã¤ã¼ãã®ããã¥ã¡ã³ãåãããã¢ã¦ãããããã¬ãã¥ã¼ããã®ã«ç¹ã«å½¹ã«ãã¡ã¾ãã
ããã«è²¢ç®ãã¦ããã¦ãããã¨ãThomas Stratmann!
ãã詳ããæ å ±:
ãã©ã¼ããã¿ã¼ã®APIãå¤ããã¾ãã
ããæè»ãªå®å ¨ã«æ°ãããã©ã¼ããã¿ã¼APIã追å ããã¾ããã
- ããªãã欲ããã¤ãã³ãã®ã¿ãåãåãã¾ã
- ã¡ã½ããã¯ãå ·ä½çãªãã©ã¡ã¼ã¿ã¼ã§ã¯ãªãéç¥ãªãã¸ã§ã¯ããåãåãã¾ãããªã®ã§å¾æ¹äºææ§ã®ããã¼ã«ã®ã£ã¨ãæ°ããéç¥ãã¼ã¿ã追å ãããã¨ãåºæ¥ãããã«ãªãã¾ãã
- ãã«ãã¼ã¡ã½ããã¯éç¥ãªãã¸ã§ã¯ãä¸ã§ä½¿ããããã«ãªã£ãã®ã§
BaseTextFormatter
ãç¶æ¿ããå¿ è¦ããªããªãã¾ãã
æ°ãããã©ã¼ããã¿ã¼ã¯ãããªæãã«ãªãã§ããã:
custom_formatter.rb
class CustomFormatter RSpec::Core::Formatters.register self, :example_started def initialize(output) @output = output end def example_started(notification) @output << "example: " << notification.example.description end end
å¤ã2.xã®ãã©ã¼ããã¿ã¼APIãå¼ãç¶ããµãã¼ãããããã«rspec-legacy_formatters gemãæä¾ããã¾ãã
ãã®å½¹ãå¼ãåãã¦ãããã¨ãJon Roweã
ãã詳ããæ å ±:
ã¢ãµã¼ã·ã§ã³ã®è¨å®ãå¤ããã¾ãã
ã»ã¨ãã©ã®ã¦ã¼ã¶ã¼ã¯rspec-expectations
ã使ã£ã¦ãã¾ãããå®ã¯ä»ã®ãã®ã使ããã¨ãã§ãã¾ããRSpec 2.xã¯ä¸çªä¸è¬çãªä»£æ¿ãè¨å®ãªãã·ã§ã³ã§ç°¡åã«å©ç¨ãããã¨ãã§ãã¾ãã:
spec/spec_helper.rb
RSpec.configure do |config| config.expect_with :stdlib # ãããã¯ã両æ¹ã使ã config.expect_with :stdlib, :rspec end
ãã¦ããã®:stdlib
å¨ãã§æ··ä¹±ãããã¾ããã
Ruby 1.8ã§ã¯ãæ¨æºã®ã¢ãµã¼ã·ã§ã³ã¢ã¸ã¥ã¼ã«ã¯Test::Unit::Assersions
ã§ãã
1.9+ã§ã¯ãããã¯Minitesst::Assertions
ä¸ã®èãã©ããã¼ã§ã(ããªããåã«ããã ãã使ããªããã¾ãããã¾ã)ã
ãããã¦ãããã¡ã«ãminitest gemã¨test-unit gemãåæ§ã«Test::Unit::Assertions
ãå®ç¾©ãã¾ããã
RSpec 3ã§ã¯ãæã
ã¯expect_with :stdlib
ãåãé¤ãã¾ããããã®ä»£ããã«æ確ã«:test_unit
ã:minitest
ãªãã·ã§ã³ãé¸æããããã«ãã¾ãã:
spec/spec_helper.rb
RSpec.configure do |config| # for test-unit: config.expect_with :test_unit # for minitest: config.expect_with :minitest end
ãããå®è£ ãã¦ããã¦ãããã¨ãAaron Kromerã
ãã詳ããæ å ±:
ãç±æ¥ã®ã¡ã¿ãã¼ã¿å®ç¾©
RSpecã®ã¡ã¿ãã¼ã¿ã·ã¹ãã ã¯è¶
絶ã«æè»ã§ãããªãã¯ãã¹ãã¹ã¤ã¼ããããããªåãå£ã§æ±ããã¨ãã§ãã¾ãã
ãã®æ°ããè¨å®APIã¯ãããªãããç±æ¥ã®ã¡ã¿ãã¼ã¿ãå®ç¾©ãããã¨ãå¯è½ã«ãã¾ãã
ä¾ãã°ãspec/acceptance/js
以ä¸ã®å
¨ã¦ã®example groupã:js => true
ã§ã¿ã°ã¥ããããã§ãã¾ã:
spec/spec_helper.rb_
RSpec.configure do |config| config.define_derived_metadata(:file_path => %r{/spec/acceptance/js/}) do |metadata| metadata[:js] = true end end
ãã詳ããæ å ±:
ãªããªã£ããã®
ããã¤ãã®ãã®ã«ã¤ãã¦ã¯ãRSpecã®ã³ã¢ã«å«ã¾ããªããªããå ¨ã¦åãé¤ããããå¤é¨gemã¨ãã¦åãã ããã¾ãã:
- Textmateãã©ã¼ããã¿ã¼ã¯Textmate bundleã«ç§»åãã¾ããããã£ã1ã¤ã®ããã¹ãã¨ãã£ã¿ã®ããã®ãã©ã¼ããã¿ã¼ãrspec-coreã®ä¸ã«æã¤ã®ã¯çãéãã¾ããã
- RCovã¨ã®çµ±åã¯ãªããªãã¾ãããããã1.9+ã§åãããã«ã¢ãããã¼ãããããã¨ã¯ããã¾ããã§ãããæè¿ã§ã¯æã ã¯ä»£ããã«simplecovã使ããã¨ããããããã¦ã¾ãã
- ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã®
--debug
ãã¨ãã®ããã¾ãããæè¿ã§ã¯é常ã«ãããã®ãããã¬ã¼ã®é¸æè¢ãããã¾ããããã¦ããªãã¯ããããã³ãã³ãã©ã¤ã³ãã--require
(ãããã¯-r
)ãªãã·ã§ã³ã使ã£ã¦æå¹ã«ã§ãã¾ããä¾ãã°ãbyebugã使ãã«ã¯ã-rbyebug
ãã³ãã³ãã©ã¤ã³ã§æ¸¡ãã¾ãã - æã
ã¯ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã®
--line-number
ãã¨ãã®ããã¾ãããããã¯å é ã«ããå ´åæå³ãã¯ã£ãããã(--line-number 43
ã¯ãã¼ããããå ¨ã¦ã®specãã¡ã¤ã«ã®43è¡ç®ã®è¿ãã§å®ç¾©ãããexampleãé¸æããããããããã®ãã¡ã¤ã«ã®43è¡ç®ãé¢ä¿ããçç±ããªã)ãããã¶ã£ããã¼ããªæãã®path/to/spec.rb:43
ã®å½¢ã¨éè¤ãã¦ãã¾ãã its
ãæ°ããrspec-its gemã«åãã ããã¾ãããããã¯è¦ªåã«ãPeter Alfvinãã¡ã³ããã³ã¹ãããã¨ãç³ãåºã¦ããã¾ããã- Autotestã¨ã®çµ±åãæ°ããrspec-autotest gemã«åãã ããã¾ãã(ããã«ã¯ã¡ã³ããã¼ãå¿ è¦ã§ã: ã ãããã©ã³ãã£ã¢ã¯ãã¾ããã?)ã
rspec-expectations
should
è¨æ³ãæ示çã«æå¹ã«ããã«ä½¿ãã®ã¯deprecatedã«ãªãã¾ãã
RSpec 2.11ã§æã
ã¯æ°ããexpectãã¼ã¹ã®è¨æ³ãå°å
¥ãããã¨ã§ãã¢ã³ãã¼ããããRSpecããå¤ãæ¹åã«é²ã¿ã¯ããã¾ããã
RSpec 3ã§ã¯ãæã
ã¯should
è¨æ³ããã¡ç¶ãã¾ããããã¦ããã¯ããã©ã«ãã§å©ç¨å¯è½ã§ãããããããªããæ示çã«ãããæå¹ã«ããªãå ´åãããªãã¯deprecation warningãåãã¨ãã§ãããã
ããã¯ãå¤ããã¥ã¼ããªã¢ã«ã§æ°ããRSpecãå¦ã³ã¯ããã人ã®æ··ä¹±ãé¿ããRSpec 4ã§ããã©ã«ãã§ãããç¡å¹ã«ãããã(ãããã¯å¥ã®gemã¨ãã¦åãé¢ãå¯è½æ§ãããã¾ã)ã®ç¶æ³ãæ´ããã§ãããã
æã
ã¯expect
è¨æ³ãRSpecã® "main" ã®è¨æ³ã«ãããã¨ãããã¾ããããããããªããå¤ãshould
ãã¼ã¹ã®è¨æ³ã好ããªããããã使ãç¶ãããã¨ãã§ãã¾ã: æã
ã«ã¯ãããç¡ããã¦ãã¾ãè¨ç»ã¯ããã¾ããã
ãããå®è£ ãã¦ããã¦ãããã¨ãSam Phippenã
ãã詳ããæ å ±:
ãããã£ã¼åæå¼
RSpec 3ã§ã¯ãããªãã¯è¤æ°ã®ãããã£ãand
ã¨or
ã使ã£ã¦ã¤ãªãã¦ä¸ç·ã«ãããã¨ãã§ãã¾ã:
compound_examples.rb
# ãã®2ã¤ã®expectationsã... expect(alphabet).to start_with("a") expect(alphabet).to end_with("z") # ...ã¬ããã£ã³ã³ãã¦1ã¤ã®å¼ã«ãªãã expect(alphabet).to start_with("a").and end_with("z") # ãã¡ãã`or`ãåãããã«ä½¿ãã¾ã: expect(stoplight.color).to eq("red").or eq("green").or eq("yellow")
ãããã¯&
ã¨|
æ¼ç®åã«ã¨ã¤ãªã¢ã¹ããã¦ãã¾ãã
compound_operator_examples.rb
expect(alphabet).to start_with("a") & end_with("z") expect(stoplight.color).to eq("red") | eq("green") | eq("yellow")
ãã®æ©è½ãææ¡ãå®è£
ãã¦ããã¦ãããã¨ãEloy Espinacoãããã¦&
ã¨|
æ¼ç®åã§ãããæ¡å¼µãã¦ããã¦ãããã¨ãAdam Farhiã
ãã詳ããæ å ±:
- New in RSpec 3: Composable Matchers (訳注: æ¥æ¬èªè¨³ãã¾ããâRSpec 3ã®æ°æ©è½: ã³ã³ãã¼ã¶ãã«ãããã£ã¼)
- Documentation
ã³ã³ãã¼ã¶ãã«ãããã£ã¼
RSpec 3ã¯ããããã£ã¼ãå¼æ°ã¨ãã¦ä»ã®ãããã£ã¼ã«æ¸¡ããã¨ã§ãããªãã®ææã詳細ã«è¡¨ç¾ãããã¨ãå¯è½ã«ãã¾ãã
composed_matcher_examples.rb
s = "food" expect { s = "barn" }.to change { s }. from( a_string_matching(/foo/) ). to( a_string_matching(/bar/) ) expect { |probe| "food".tap(&probe) }.to yield_with_args( a_string_starting_with("f") )
ã³ã¼ãã®è¡¨ç¾ã¨å¤±ææã®ã¡ãã»ã¼ã¸ã®å¯èªæ§ãåä¸ããããããã»ã¨ãã©ã®ãããã£ã¼ã¯ããããã®å¼ãå¼æ°ã¨ãã¦æ¸¡ãããã¨ãããã¡ãã¨èªãããããªã¨ã¤ãªã¢ã¹ããã£ã¦ãã¾ãã
ãã詳ããæ å ±:
- New in RSpec 3: Composable Matchers (訳注: æ¥æ¬èªè¨³ãã¾ããâRSpec 3ã®æ°æ©è½: ã³ã³ãã¼ã¶ãã«ãããã£ã¼)
- rspec-expectations #280 - original discussion
- rspec-expectations #393 - implementation
- API Documentation (including list of matcher aliases)
- Relish Documentation
match
ãããã£ã¼ããã¼ã¿æ§é ã«å¯¾ãã¦ä½¿ããã¨ãã§ãã¾ã
RSpec 3以åã§ã¯ãmatch
ãããã£ã¼ã¯#match
ã¡ã½ããã使ã£ã¦æåå/æ£è¦è¡¨ç¾ã®ãããã³ã°ãè¡ãããã«åå¨ãã¦ãã¾ãã:
match_examples.rb
expect("food").to match("foo") expect("food").to match(/foo/)
RSpec 3ã§ã¯ãããã¯ä»»æã®ãã¹ãããé å/ããã·ã¥ã®ãã¼ã¿æ§é ã®ãããã³ã°ã追å ã§ãµãã¼ããã¾ãã ãã¹ãã®ä¸ã®ã©ã®ã¬ãã«ã§ããæå¾ ããå¤ããããã£ã¼ã使ã£ã¦è¡¨ç¾ãããã¨ãã§ãã¾ã:
match_data_structure_example.rb
hash = { :a => { :b => ["foo", 5], :c => { :d => 2.05 } } } expect(hash).to match( :a => { :b => a_collection_containing_exactly( an_instance_of(Fixnum), a_string_starting_with("f") ), :c => { :d => (a_value < 3) } } )
ãã詳ããæ å ±:
- New in RSpec 3: Composable Matchers (訳注: æ¥æ¬èªè¨³ãã¾ããâRSpec 3ã®æ°æ©è½: ã³ã³ãã¼ã¶ãã«ãããã£ã¼)
æ°ããall
ãããã£ã¼
ãã®ãããã£ã¼ãã¤ããã°ãããªãã¯ã³ã¬ã¯ã·ã§ã³ã®ä¸ã®å ¨ã¦ã®è¦ç´ ã®ä½ããtrueã§ãããã¨ãè¨è¿°åºæ¥ãããã«ãªãã¾ããå¼æ°ã¨ãã¦ãããã£ã¼ã渡ãã¾ã:
all_example.rb
expect([1, 3, 5]).to all( be_odd )
ããã«è²¢ç®ãã¦ããã¦ãããã¨ãAdam Farhi
ãã詳ããæ å ±:
æ°ããoutput
ãããã£ã¼
ãã®ãããã£ã¼ãã¤ããã¨ããã®ãããã¯ãstdoutãããã¯stderrã«æ¸ãå 容ãè¨è¿°ã§ããããã«ãªãã¾ãã
output_examples.rb
expect { print "foo" }.to output("foo").to_stdout expect { print "foo" }.to output(/fo/).to_stdout expect { warn "bar" }.to output(/bar/).to_stderr
ãããææ¡ãã¦ããã¦ãããã¨ãMatthias Günther(ããã«ã¹ã¿ã¼ãããã¡ã¦ããã)ã ããã¦ãã®æ©è½ããã£ããã·ã¥ã©ã¤ã³ã«å°ãã¦ããã¦ãããã¨ãLuca Petteã
ãã詳ããæ å ±:
æ°ããbe_between
ãããã£ã¼
RSpec 2ã¯åçãªè¿°èªãµãã¼ãã使ããbetween?
ãå®è£
ãã¦ããªãã¸ã§ã¯ãã«å¯¾ãã¦be_between
ãããã£ã¼ãæä¾ãã¦ãã¾ããã
RSpec 3ã§ãæã
ã¯be_between
ãããã£ã¼ãä¸ç´å¸æ°ã«æ¼ãä¸ãããã¨ã«ãã¾ãããããã«ã¯ããã¤ãã®ããé¢ãããã¾ã:
- 失ææã®ã¡ãã»ã¼ã¸ãåã«ã
between?(1, 2)
ãfalse
ãè¿ãããã¨ããããããããªãã¾ãã - ããã¯ã
between?
ãå®è£ ãã¦ããªãã¦ããæ¯è¼æ¼ç®åãå®è£ ãããªãã¸ã§ã¯ãã§åä½ãã¾ã(e.g.<
,<=
,>
,>=
) - ããã¯
inclusive
ã¨exclusive
2ã¤ã®ã¢ã¼ããæä¾ãã¾ãã
be_between_examples.rb
# `Comparable#between?`ã®ããã«ãããã©ã«ãã ã¨inclusiveã§ã expect(10).to be_between(5, 10) # ...ããããããªãã¯ãããexclusiveã«ãããã¨ãã§ãã¾ã: expect(10).not_to be_between(5, 10).exclusive # ...ãããã¯æ確ã«inclusiveã®ã©ãã«ãä»ãããã¨ãã§ãã¾ã: expect(10).to be_between(5, 10).inclusive
ããã«è²¢ç®ãã¦ããã¦ãããã¨ãErik Michaels-Oberã ããã¦ãããã¦ããã¦ãããã¨ãPedro Gimenezã
ãã詳ããæ å ±:
Booleanãããã£ã¼ã®ååããããã¾ãã
RSpec 2ã¯ãã¢ã®ãããã£ã¼ãæã£ã¦ãã¾ãã(be_true
ã¨be_false
)ããã¯Rubyã®æ¡ä»¶ã®æå³ãåæ ãã¦ãã¾ã: be_true
ã¯nil
ãfalse
以å¤ã®å
¨ã¦ã®å¤ã§éãã¾ããããã¦be_false
ã¯nil
ãfalse
ã®å ´åéãã¾ãã
RSpec 3ã§ã¯ãæã
ã¯ããããããæ確ãªæå³ã«ãªãããbe_truthy
ã¨be_falsey
(ãããã¯be_falsy
ãããããªãããã®ã¹ããªã³ã°ã®ã»ãã好ã¿ãªã)ã«å¤æ´ãã¾ããã
ããã¦be true
/be false
ã¨ã®æ··ä¹±ããªããã¾ãã(be_true
/be_false
ã¨åãããã«èªããããã»ãã¨ã«true
/false
ã®ã¨ãããéããªã)ã
ãããå®è£ ãã¦ããã¦ãããã¨ãSam Phippen
ãã詳ããæ å ±:
match_array
ãããã£ã¼ãcontain_exactly
ã¨ãã¦å©ç¨ã§ããããã«ãªã£ã
RSpecã¯é·ãéããã®ãããã£ã¼ãããªãã2ã¤ã®é
åã®å
容ãé çªãæ°ã«ããã«æ¯è¼ãããã¨ã«ä½¿ããããã«ãã¦ãã¾ããã
ãã¨ãã¨ã¯ãããã¯å¤ãshould
è¨æ³ã§=~
ã使ããã¨ã§å©ç¨ãããã¨ãã§ãã¾ãã:
match_array_operator_example.rb
[2, 1, 3].should =~ [1, 2, 3]
ãã®å¾ãæã
ãexpectè¨æ³ã追å ããã¨ããæ¼ç®åã®ãããã£ã¼ãæ°ããè¨æ³ã«æã¡è¾¼ã¾ãªããã¨ã決ãã¾ãããããã¦ãã®ãããã£ã¼ã®ãã¨ãmatch_array
ã¨å¼ã¶ãã¨ã«ãã¾ãã:
match_array_example.rb
expect([2, 1, 3]).to match_array([1, 2, 3])
match_array
ã¯ãã®ã¨ãç§éãä¸çªããã¨èããååã§ããã
ãããæã
ã¯ãããææ¾ã§åã¶ããã«ã¯ããã¾ããã§ãã: "match"ã¯ä¸æ確ãªè¨èé£ãã§ãããã®ãããã£ã¼ã¯é
å以å¤ã®ç¨®é¡ã®ã³ã¬ã¯ã·ã§ã³ã§ãããããã¨ãæå³ãã¦ãã¾ãã
RSpec 3ã§æã
ã¯ãã®ãããã£ã¼ã«ããããååãã¤ãã¾ãã:
contain_exactly_example.rb
expect([2, 1, 3]).to contain_exactly(1, 2, 3)
æ°ãã¤ãã¦æ¬²ããã®ã¯match_array
ã¯deperecatedã§ã¯ãªããã¨ã§ãã
match_array
ã¯1ã¤ã®é
åãå¼æ°ã§åãåãä¸æ¹ãcontain_exactly
ã¯å±éãããè¦ç´ ãåå¥ã«åãåã(訳注: *splatã©ã訳ãã)ãã¨ä»¥å¤ã¯ããã®2ã¤ã®ã¡ã½ããã¯ã¾ã£ããåãã§ãã
ãã詳ããæ å ±:
ã³ã¬ã¯ã·ã§ã³æ°ãããã£ã¼ãrspec-collection_matchers
gemã«åãã ããã¾ãã
ã³ã¬ã¯ã·ã§ã³æ°ãããã£ã¼âhave(x).items
ãhave_at_least(y).items
ããã«have_at_most(z).items
âã¯RSpecã®éæ³ãã¿ã¦æ··ä¹±ãããã¼ãã®1ã¤ã§ããã
ãããã¯åãã ããã¦ãrspec-collection_matchers
gemã«ç§»ããã¾ããã
親åã«ãHugo Baraúnaãã¡ã³ããã³ã¹ããã®ã«åä¹ããããã¦ããã¾ããã
ããä¸è¬çãªä»£æ¿ã¯ã³ã¬ã¯ã·ã§ã³ã®sizeã«æ¤è¨¼ãç½®ããã¨ã§ã:
collection_matcher_examples.rb
expect(list).to have(3).items # ...ããã¯ãã®ããã«æ¸ãã¾ã: expect(list.size).to eq(3) expect(list).to have_at_least(3).items # ...ããã¯ãã®ããã«æ¸ãã¾ã: expect(list.size).to be >= 3 expect(list).to have_at_most(3).items # ...ããã¯ãã®ããã«æ¸ãã¾ã: expect(list.size).to be <= 3
Minitestã¨ã®çµ±åã®ã«ã¤ã¼ã³
RSpec 2.xã§ã¯ãrspec-expectationsã¯èªåçã«èªèº«ãMiniTest::Unit::TestCase
ãTest::Unit::TestCase
ã«includeãã¾ããããªã®ã§ããªãã¯åã«MinitestãTest::Unitããã¼ãããã ãã§rspec-expectationsã使ããã¨ãã§ãã¾ããã
RSpec 3ã§ã¯ãæã ã¯ãã®çµ±åã2ã¤ã®æ¹æ³ã§ã¢ãããã¼ããã¾ãã:
- Minitest 4(ãããã¯ãã以ä¸)ã¨Test::Unitã¨ã®çµ±åãèªåçã«è¡ãããªããªãã¾ãããããããªããrspec-expectationsããã®ãããªç°å¢ã§ä½¿ããªããããªãã¯
RSpec::Matchers
ãããªãèªèº«ã§ã¤ã³ã¯ã«ã¼ãããå¿ è¦ãããã¾ãã - Minitest 5ã¨ã®ããããçµ±åãæä¾ããã¾ããããããããªãã¯
require 'rspec/expectations/minitest_integration'
ã§ãããæ示çã«ãã¼ãããå¿ è¦ãããã¾ã
ãã詳ããæ å ±:
ãããã£ã¼ãããã³ã«ã®å¤æ´
ä¸ã§è¿°ã¹ãããã«ãRSpec 3ã§ã¯ãæã
ã¯should
ãrspec-expectationsã®ã¡ã¤ã³ã®è¨æ³ã«ãããã¨ãããã¾ããã
æã
ã¯ãããåæ ãã¦ãããã£ã¼ãããã³ã«ãã¢ãããã¼ããã¾ãã:
failure_message_for_should
ã¯ãã¾failure_message
failure_message_for_should_not
ã¯ãã¾failure_message_when_negated
match_for_should
(ã«ã¹ã¿ã ãããã£DSLã®match
ã¨ã¤ãªã¢ã¹)ã«ã¯å¤ããã®ãã®ã¯ç¨æããããã¨ãã®ãããã¾ãã(åã«match
ã使ã£ã¦ãã ãã)- ã«ã¹ã¿ã ãããã£DSLã®
match_for_should_not
ã¯ãã¾match_when_negated
ããã«å ããæã
ã¯supports_block_expectations?
ãæ°ãã追å ãã¾ããããããã£ã¼ãããã³ã«ã®ãªãã·ã§ã³é¨åã§ãã
ããã¯ããã¦ã¼ã¶ã¼ãvalueãããã£ããããã¯expectationå¼ä¸ã§ééã£ã¦ã¤ãã£ãã¨ãã«åãããããã¨ã©ã¼ãä¸ãã¾ãã
ä¾ãã°ããã®å¤æ´ã®åã§ã¯ãbe_nil
ã®ãããªãããã£ã使ãã¨ãã«ãblockãexpect
ã«æ¸¡ããã¨ã§ã誤ã£ãçµæãå°ãã¦ãã¾ãã¾ãã:
block_expectation_gotcha.rb
expect { foo.bar }.not_to be_nil # ...ããã¯æ¬¡ã¨åæ§ã®æå³ã§ã: block = lambda { foo.bar } expect(block).not_to be_nil # ...ãããããããã¯ã¯nilã§ã¯ããã¾ãã(ãã¨ã`foo.bar`ãnilãè¿ãã¨ãã¦ã)ã # ãªã®ã§ã¦ã¼ã¶ã¼ã®æå³ã次ã®ãããªãã®ã ã£ãã¨ãã¦ããexpectationãéã£ã¦ãã¾ãã¾ã: expect(foo.bar).not_to be_nil
æ°ãã¤ãã¦ã»ããã®ã¯ãsupports_block_expectations?
ã¯ãããã£ã¼ãããã³ã«ã®ãªãã·ã§ã³ã®é¨åã ã¨ãããã¨ã§ãã
ãããã¯expectationå¼ã§ä½¿ã£ã¦ã»ãããªããããã£ã®ããã«ãããã¦ãããå®ç¾©ããå¿
è¦ã¯ããã¾ããã
ãã詳ããæ å ±:
- rspec-expectations #270 - original discussion
- rspec-expectations #373 - implementation
- rspec-expectations #530 - original discussion of supports_block_expectations?
- rspec-expectations #530 - implementation of supports_block_expectations?
rspec-mocks
ã¢ã³ãã¼ãããããã·ã³ã¿ãã¯ã¹ãæ示çã«æå¹ã«ããã«ä½¿ãã®ã¯deprecated
rspec-expectationsã¨å
±ã«ãæã
㯠rspec-mocks ãã¼ãã¢ã³ãã¼ãããã®ã·ã³ã¿ãã¯ã¹ã¸åãã¦é²ã¿ã¯ããã¾ããã
ãããã¯2.14ããå°å
¥ããã¾ããã
RSpec 3ã§ã¯ãããªãããªãªã¸ãã«ã®è¨æ³(e.g. obj.stub
ãobj.should_receive
ãetc)ãæ示çã«æå¹ã«ããã«ä½¿ãã¨deprecation warningãåºãããã«ãªãã¾ãã(rspec-expectationã®æ°ããè¨æ³ã¨åæ§ã§ã)ã
ãããå®è£ ãã¦ããã¦ãããã¨ãSam Phippen
receive_messages
ã¨receive_message_chain
ã®ããã®æ°ããè¨æ³
ãã¨ãã¨ã®ã¢ã³ãã¼ãããã®è¨æ³ã«ã¯2.14ã§ãªãªã¼ã¹ãããæ°ããè¨æ³ã«ã¯ããã¦ããæ©è½ãããã¤ãããã¾ããã
æã
ã¯RSpec 3ã§ããã«å¯¾ãã¦2ã¤ã®æ°ããAPIãæ示ãã¾ã: receive_messages
ã¨receive_message_chain
ã§ãã
examples.rb
# å¤ãè¨æ³: object.stub(:foo => 1, :bar => 2) # æ°ããè¨æ³: allow(object).to receive_messages(:foo => 1, :bar => 2) # å¤ãè¨æ³: object.stub_chain(:foo, :bar, :bazz).and_return(3) # æ°ããè¨æ³: allow(object).to receive_message_chain(:foo, :bar, :bazz).and_return(3)
ãããã®æ°ããAPIã®å©ç¹ã®1ã¤ããããã¯expect
ã¨çµã¿åããã¦ããåãããã«åããã¨ã§ããä¸æ¹ã§å¤ãè¨æ³ã®stub(hash)
ãstub_chain
ã§ã¯ããã¨åããã¨ã¯ã§ãã¾ããã
ãããå®è£ ãã¦ããã¦ãããã¨ãJon Roweã¨Sam Phippenã
- Documentation for
receive_messages
- Documentation for
receive_message_chain
- rspec-mocks #368 - discussion of
receive_messages
- rspec-mocks #399 - implementation of
receive_messages
- rspec-mocks #464 - discussion of
receive_message_chain
- rspec-mocks #467 - implementation of
receive_message_chain
double
ã®ã¨ã¤ãªã¢ã¹ã®mock
ã¨stub
ãåãé¤ãã¾ãã
æ´å²ççµç·¯ã«ãããrspec-mocksã¯ãã¹ãããã«ãä½ãããã«3ã¤ã®ã¡ã½ãããæä¾ãã¦ãã¾ãã: mock
ãstub
ã¨double
ã
RSpec 3ã§ã¯ãæã
ã¯stub
ã¨mock
ãã¨ãã®ããåãªãdouble
ã使ããã¨ãé¸æãã¾ãããããã¦double
ã®ä¸ã«ãããªãæ©è½ãä½ç³»ã ã¦ã¦ãããã¨ã«ãã¾ãã(veryfying doubleã¿ãããªãä¸ã®æ¹ãã¿ã¦ãã ãã)ã
ãã¡ãããRSpec 3ã¯double
ã«ãããã¦mock
ã¨stub
ã®ã¨ã¤ãªã¢ã¹ãæä¾ãã¾ããããããªãããããã使ãç¶ããããªãã°ããããã®ã¨ã¤ãªã¢ã¹ãå®ç¾©ããã®ã¯ç°¡åã§ã:
spec/spec_helper.rb
module DoubleAliases def mock(*args, &block) double(*args, &block) end alias stub mock end RSpec.configure do |config| config.include DoubleAliases end
ãããå®è£ ãã¦ããã¦ãããã¨ãSam Phippenã
ãã詳ããæ å ±:
doubleããæ¤è¨¼ãã
ããªããå®éã«åå¨ããã¡ã½ããã®ã¿ãstubãããã¯ã¢ãã¯ãã¦ãããã¨ãä¿è¨¼ãããããæ°ãã種é¡ã®doubleã追å ããã¾ãããããã¯æ¸¡ãããå¼æ°ã宣è¨ãããã¡ã½ããã·ã°ããã£ã«ãã£ã¦ç¢ºèªãã¾ãã
instance_double
ã¨class_double
ãããã¦object_double
ã®doubleãã¯ããããã®æ¡ä»¶ãæºããããªãæãã¹ã¦ã«ããã¦ä¾å¤ãæãã¾ãã
ããã¯ã©ã¹ããã¼ãããã¦ããªãã¨ã(é常ã¯ã¦ããããã¹ããç¬ç«ãã¦å®è¡ãã¦ããé)ãä¾å¤ãçºçãããã¨ã¯ããã¾ããã
ããã¯è¤éãªåä½ã§ãããããç¬ç«ããã¦ããããã¹ãã®é度ãé«ããã¤ã³ãã°ã¬ã¼ã·ã§ã³ãã¹ã(ãããã¯åã·ã¹ãã )ã«è¿ã¥ãã«ã¤ãä¿¡é ¼ã§ããé常ã«å¼·åã§ãã
ããã¯å¤§å¤å¥å¦ãªåä½ã«è¦ããããããã¾ããããç¬ç«ããã¦ããããã¹ãã«é度ã¨ãã¤ã³ãã°ã¬ã¼ã·ã§ã³ãã¹ã(ãããã¯åã·ã¹ãã )ã«è¿ãä¿¡é ¼æ§ãä¸ãã¾ãã(訳注:ãããã¸ã訳ãä¸å®)
ãããã®æ°ãããããã¯ãã«ãªdoubleåã使ãã¹ãã§ã¯ãªãçç±ã¯ãã¾ããªãã§ãããã
ãã®æ©è½ã®ã¢ã¤ãã¢ã¨å®è£ ããããã¨ãXavier Shay
ãã詳ããæ å ±:
é¨åçãªdoubleã®æ¤è¨¼ã®è¨å®ãªãã·ã§ã³
doubleããæ¤è¨¼ããåä½ã¯é¨åçãªdoubleã«ããã¦ã¯ã°ãã¼ãã«ã«æå¹ã«ãªã£ã¦ãã¾ãã
(é¨åçãªdoubleã¨ã¯ããªããæ¢åã®ãªãã¸ã§ã¯ããmockãããã¯stubããã¨ãã§ã: expect(MyClass).to receive(:some_message)
)
spec/spec_helper.rb
RSpec.configure do |config| config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end end
æã ã¯ããªãããã®è¨å®ãå ¨ã¦ã®æ°ããã³ã¼ãã§æå¹ã«ãããã¨ãæ¨å¥¨ãã¦ãã¾ãã
ã¹ã³ã¼ããå¤ããã¾ãã
rspec-mocksã®ãªãã¬ã¼ã·ã§ã³ã¯ããã¹ããã¨ã®ã©ã¤ããµã¤ã¯ã«ã念é ã«è¨è¨ããã¾ããã ããã¯RSpec 2ã§ããã¥ã¡ã³ãåããã¾ããããå®è¡æã«å¸¸ã«ãããæ示çã«å¼·å¶ãã¦ããããã§ã¯ããã¾ããã§ãããããã¦æã ã¯ã¨ãã©ãã ã¦ã¼ã¶ã¼ãrspec-mocksã®æ©è½ããã¹ããã¨ã®ã©ã¤ããµã¤ã¯ã«ã®å¤ãã使ããã¨è©¦ã¿ãã¨ãã«ã¤ãã¦ã®ãã°ã¬ãã¼ããåãåãã¾ããã
RSpec 3ã§ã¯ãæã ã¯ãããããå³ãããããã®ã©ã¤ããµã¤ã¯ã«ãå®è¡æã«æ示çã«å¼·å¶ãã¾ã:
- rspec-mocksã®æ©è½ã
before(:context)
ããã¯(ãããã¯current exampleãåå¨ããªãä»ã®ã©ã®context)ãã使ã使ãæ¹ã¯ãµãã¼ãããã¾ããã - ãã¹ãããã«ã¯1ã¤ã®exampleã«ãã使ããªããªãã¾ãããããããªãããã¹ãããã«ããããä½ãããexampleã®å¤ãã使ããã¨ãããã¿ãå ´å(e.g. ãã£ãããããã¯ã©ã¹ã¢ããªãã¥ã¼ãã«ä»£å ¥ãã¦ããã以éã®exampleã§ããã使ã£ãå ´å)ãããªãã¯ã¨ã©ã¼ãããã¨ãã§ãããã
ãã¨ãæã
ã¯ããªãã«ä»»æã®å ´æã§ä¸æçãªã¹ã³ã¼ããã¤ããããããªæ°ããAPIãæä¾ãã¾ã(ä¾ãã°before(:context)
ããã¯ã¨ã):
my_web_crawler_spec.rb
describe MyWebCrawler do before(:context) do RSpec::Mocks.with_temporary_scope do allow(MyWebCrawler).to receive(:crawl_depth_limit).and_return(5) @crawl_results = MyWebCrawler.perform_crawl_on("http://some-host.com/") end # ãããã¯ãå®äºããã¨æ¤è¨¼ã¨ãªã»ãããèµ·ãã end # ... end
ãããã®å¤æ´ãå®è£
ãããããã¦ããã¦ãããã¨ãSam Phippenã
ããã¦with_temporary_scope
ã®æ©è½ãææ¡ãã¦ããã¦ãããã¨ãSebastian SkaÅackiã
ãã詳ããæ å ±:
any_instance
ã®ã¤ã³ããªã¡ã³ãã¼ã·ã§ã³ãããã¯ãã¬ã·ã¼ãã渡ã
ã¡ã½ããã¹ã¿ãã«ã¤ã³ããªã¡ã³ãã¼ã·ã§ã³ãããã¯ãæä¾ãããã¨ã¯ããªãã¸ã§ã¯ãã®ç¶æ
ã«å¯ããããªè¨ç®ãè¡ãã®ã«å½¹ã«ãã¤ã§ãããã
RSpec 2ã§any_instance
ã使ãã¨ããæ®å¿µãªããã·ã³ãã«ã«ãããè¡ãæ¹æ³ã¯ããã¾ããã§ããã
RSpec 3ã§ã¯ãã¬ã·ã¼ãã¼ã¯æåã®å¼æ°ã¨ãã¦any_instance
ã®ã¤ã³ããªã¡ã³ãã¼ã·ã§ã³ãããã¯ã«ããã£ã¦ããã®ã§ããããç°¡åã«è¡ããã¨ãã§ãã¾ã:
any_instance_example.rb
allow_any_instance_of(Employee).to receive(:salary) do |employee, currency| usd_amount = 50_000 + (10_000 * employee.years_worked) currency.from_usd(usd_amount) end employee = Employee.find(23) salary = employee.salary(Currency.find(:CAD))
ãããå®è£ ãã¦ããã¦ãããã¨ãSam Phippenã
ãã詳ããæ å ±:
rpsec-rails
ãã¡ã¤ã«ã¿ã¤ãã«åºã¥ãæ¨æ¸¬ãããã©ã«ãã§ç¡å¹ã«ãªãã¾ãã
rspec-railsã¯specãã¡ã¤ã«ã®ãã¡ã¤ã«ã·ã¹ãã ä¸ã®å ´æã«ãã¨ã¥ãèªåçã«specã«ã¡ã¿ãã¼ã¿ã追å ãã¾ãã ããã¯æ°è¦ã¦ã¼ã¶ã¼ã«æ··ä¹±ãããããã¾ããããã¦ããã好ã¾ãªãããã©ã³ã®ã¦ã¼ã¶ã¼ããã¾ãã RSpec 3ã§ã¯ããã®åä½ãæ示çã«æå¹ã«ããããã«ãªãã¾ãã:
spec/spec_helper.rb
RSpec.configure do |config| config.infer_spec_type_from_file_location! end
æµè¡ãã¦ãããã¥ã¼ããªã¢ã«ã§ã¯ãã®åä½ãåæã«ãã¦ãããããããã©ã«ãã§çæãããè¨å®ãã¡ã¤ã«ã¯ãããæå¹ã«ããã¾ã¾ã§ãã èªåçãªæ¨æ¸¬ã使ããã«æ示çã«specã«ã¿ã°ä»ãããããã«ã¯ãtypeã¡ã¿ãã¼ã¿ãè¨å®ãã¦ãã ãã:
things_controller_spec.rb
RSpec.describe ThingsController, type: :controller do # spec/controllers以ä¸ã«ç½®ãã®ã¨åãæå³ end
使ç¨ã§ããtypeã®éãã«ã¤ãã¦ã¯ãããããã®specã®åãã¨ã«ããã¥ã¡ã³ãåããã¦ãã¾ãããã¨ãã°controller specsã®ããã®ããã¥ã¡ã³ããªã©ã
ãã詳ããæ å ±:
activemodelã®ãµãã¼ããåãã ããã¾ãã
mock_model
ã¨stub_model
ãrspec-activemodel-mocks gemã«åãã ããã¾ããã
åãåºãã¦ããã¦ãããã¦æ°ããgemã®ã¡ã³ããã³ã¹ãç³ãåºã¦ããã¦ãããã¨ãThomas Holmesã
webratã®ãµãã¼ãããªããªãã¾ãã
Webratã®ãµãã¼ããã¨ãã®ãããã¾ãããå¤ããã«capybaraã使ã£ã¦ãã ããã
å¿åcontrollerã®ã«ã¤ã¼ã³
rspec-railsã¯é·ãéãããªãã«ãã¹ãç¨ã®å¿åã®ã³ã³ããã¼ã©ãä½ããã¨ãå¯è½ã«ãã¦ãã¾ããã RSpec 3ã§ã¯ããããã¯ããã¤ãã®ã«ã¤ã¼ã³ãæ½ããã¾ãã:
- ããã©ã«ãã§ãå¿åã³ã³ããã¼ã©ã¯
ApplicationController
ãç¶æ¿ããã説æãã¦ãã対象ã®ã¯ã©ã¹ãç¶æ¿ããããã«ãªãã¾ããããã®åä½ã¯infer_base_class_for_anonymous_controllers
ã®è¨å®ãªãã·ã§ã³ã§ç¡å¹ã«ãããã¨ãåºæ¥ã¾ãã - æ¨æºçã§ã¯ãªãã³ã³ããã¹ãã§ããã使ã£ãã¨ãã«ã¤ãã¦å¤ãã®ãã°ãã£ãã¯ã¹ãè¡ããã¾ããããã¨ãã°æ½è±¡çãªè¦ªã
ApplicationController
ãç¶æ¿ããªãã£ãå ´åãªã©ãããããªããéå»ã«å¿åã³ã³ããã¼ã©çµ¡ã¿ã®åé¡ãçµé¨ãã¦ãããªããä»ãå度ããã使ã£ã¦ã¿ãããæ©ä¼ã§ãã
ãã詳ããæ å ±:
- Documentation - Anonymous controllers
- rspec-rails #893 - Enable infering base class by default
- rspec-rails #905 - Fix anonymous controller route helpers
- rspec-rails #924 - Donât assume presence of ApplicationController
æå¾ã«
ãã¤ãã®ããã«ãå ¨ã¦ã®changelogsã¯ããããã®ãµãããã¸ã§ã¯ãã§è¦ããã¨ãã§ãã¾ã:
RSpec 3ã¯ããã4å¹´ã§ã¯ããã¦ã®ã¡ã¸ã£ã¼ãªãªã¼ã¹ã§ãã ãã®å¤§ä»äºã¯å¤§å¢ã®ã³ã³ããªãã¥ã¼ã¿ã«ãã£ã¦æããã¾ããã ããªããRSpecãã©ã使ããã¨ããæã ã¯ããªããæ°ããå¤æ´ãæã 以ä¸ã«æ°ã«å ¥ã£ã¦ããããã¨ãé¡ã£ã¦ãã¾ãã
ãã®ããã°è¨äºãæ¸ãã®ãå©ãã¦ããã¦ãããã¨ãXavier ShayãJon Roweãæ ¡æ£ããããã¨ãSam PhippenãAaron Kromer