Technofara

Golangエンジニア!仕事で必要になって勉強した事とか、新しい事とか色々まとめたりを緩くやります。技術系と思考系だけにしておきます、

せっかくなのでRails5でRspecした!

http://shinofara.hateblo.jp/entry/2016/03/16/014844
の続き

はじめてのbundler

$ gem install bundler
$ bundle install --path vendor/bundle

spec/ dirをつくる

$ rails generate rspec:install
Running via Spring preloader in process 40013
      create  .rspec                         
      create  spec                           
      create  spec/spec_helper.rb            
      create  spec/rails_helper.rb           

試しに実行

$ bundle exec rspec
No examples found.                                               
                                                                 
                                                                 
Finished in 0.00022 seconds (files took 0.06497 seconds to load) 
0 examples, 0 failures                                           

テスト無いから当然やけど!

試しにモデルを作って、テストしてみよーっ

rails generate model user
Running via Spring preloader in process 40126            
      invoke  active_record                              
      create    db/migrate/20160316141612_create_users.rb
      create    app/models/user.rb                       
      invoke    rspec                                    
      create      spec/models/user_spec.rb               

かってにusersにしてくれる可愛いやつ

$ bundle exec rspec
/path/to/schema.rb doesn't exist yet. Run `rails db:migrate` to create it, then try again..........

怒られたし

ファイル無いから作れ

はい

$ rails db:migrate
== 20160316141612 CreateUsers: migrating ======================================
-- create_table(:users)                                                        
   -> 0.0010s                                                                  
== 20160316141612 CreateUsers: migrated (0.0011s) =============================

users tableが出来たようだ!

どこに?

$ cat config/database.yml

を見る限り db/development.sqlite3 にあるようだ defaultはとりあえずSqlLiteなのね。

はい勧めます。

$ bundle exec rspec
ending: (Failures listed here are expected and do not affect your suite's status)                        
                                                                                                         
 1) User add some examples to (or delete) /path/to/<APPNAME>/spec/models/user_spec.rb  
    # Not yet implemented                                                                                
    # ./spec/models/user_spec.rb:4                                                                       
                                                                                                         
                                                                                                         
inished in 0.00042 seconds (files took 2.35 seconds to load)                                             
 example, 0 failures, 1 pending                                                                          

まだテストの中身ないからペンディング....

失敗するように書き換えてみた。 https://github.com/shinofara/sample-rail5/pull/1/commits/f4fd4d1865556cfefdde75aac5be1ce61746eb49
こんな感じ

$ bundle exec rspec                                     
F                                                                             
                                                                              
Failures:                                                                     
                                                                              
  1) User 作成できるか                                                        
     Failure/Error: expect(u).to be_valid                                     
                                                                              
     NoMethodError:                                                           
       undefined method `valid?' for nil:NilClass                             
     # ./spec/models/user_spec.rb:7:in `block (2 levels) in <top (required)>' 
                                                                              
Finished in 0.00096 seconds (files took 1.1 seconds to load)                  
1 example, 1 failure                                                          
                                                                              
Failed examples:                                                              
                                                                              
rspec ./spec/models/user_spec.rb:5 # User 作成できるか                                       

失敗

じゃ、最後は
https://github.com/shinofara/sample-rail5/pull/1/commits/fb0035b4e71dbfe6371b4215e1940f0b0728e5d6
こんな感じに書き換えて

$ bundle exec rspec                    
.                                                            
                                                             
Finished in 0.00318 seconds (files took 1.1 seconds to load) 
1 example, 0 failures                                        

Greeeeeeeen とりあえずRspec動かす編終了

前回との差分 https://github.com/shinofara/sample-rail5/pull/1/files