ããããã
ãã®èšäºã¯RubyãRailsãããããã£ãŠããªã人ãèªåã®ããã®ã¡ã¢ãšããŠã ãã ãæžããŸããããªãã¡ã¬ã³ã¹ãããããããªãã£ãã®ã§ãåãããŠè©ŠããŠã¿ãæãã§ãã
RSpecã¯ïŒRailsã«éããïŒRubyã§åããã¹ããã¬ãŒã ã¯ãŒã¯ãRailsã«æåããå ¥ã£ãŠãTest::Unitãããè²ã ãšè¯ããããâŠâŠãã©ãã£ã¡ã䜿ã£ãäºããªãã®ã§æ¯èŒã¯ã§ããŸããã
RubyãããªããŠRailsããå©çšããèŠç¹ãã俺çšã«ãŸãšããŸãã
ç°å¢
$ ruby -v ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux] $ rails -v Rails 3.2.1
ã€ã³ã¹ããŒã«
Gemfileã«èšè¿°ã
gem 'rspec-rails'
ã§bundle
ãããšãé¢é£GemããŸãšããŠå
¥ãã
ããšRSpecã®å ±éãã¡ã€ã«ãçšæãããå¿ èŠãããã
rails g rspec:install
ã€ã³ã¹ããŒã«ã¯ããã§ãããã
scaffold
ãã¹ããŠããããèªåã§RSpecã«å€æŽãããŠãã¿ãããscaffold
ãããšãæ¢åã®test/
ã«ã¯äœãäœãããªãã§ã代ããã«spec/
é
äžã«ãããããã¹ããäœæãããã
rails g scaffold item name:string price:integer description:text on_sales:boolean rake db:migrate
spec/
+ controllers/
+ items_controller_spec.rb
+ helpers/
+ items_helper_spec.rb
+ models/
+ item_spec.rb
+ requests/
+ items_spec.rb
+ routing/
+ items_routing_spec.rb
+ spec_helper.rb
+ views/
+ items/
+ edit.html.erb_spec.rb
+ new.html.erb_spec.rb
+ index.html.erb_spec.rb
+ show.html.erb_spec.rb
fixture
ããªããã
scaffold
ãããªããŠãåå¥ã«controller
ãmodel
ãäœæãããšããåããããªæãã§ïŒå¿
èŠãªåã ãïŒäœãããã¿ããã
ãã¹ãå®è¡
rake
ããå®è¡ããŸãã
rake spec
**............................
Pending:
Item add some examples to (or delete) /.../spec/models/item_spec.rb
# No reason given
# ./spec/models/item_spec.rb:4
ItemsHelper add some examples to (or delete) /.../spec/helpers/items_helper_spec.rb
# No reason given
# ./spec/helpers/items_helper_spec.rb:14
Finished in 0.75908 seconds
30 examples, 0 failures, 2 pending
âŠâŠã ãããã§ãã
pendingã£ãŠïŒ
ã圢容è©ã
1. æªæ±ºå®ã§ïŒä¿äºäžã§; å®ã¶ãããã§.
ãã¹ãã«å€±æã¯ããŠãªããã©ããŸã åæ Œã£ãŠã»ã©ã§ããªãç¶æ ãããªã
spec/models/item_spec.rb
describe Item do pending "add some examples to (or delete) #{__FILE__}" end
ããããã°Test::Unitã®æ¹ãModelã®è©Šéšã¯æåã¯ç©ºã ã£ãããªããã¢ãã«ãã©ãæ¯ãèããã£ãŠã®ã¯scaffold
ã®æç¹ã§ã¯ãŸã ããããªããããããªãHelperãããããªããã
ModelãšHelperã®è©Šéšã空ã«ïŒpending
ãåé€ïŒãããšããªãŒã«ã°ãªãŒã³ã«ãªãã
............................
Finished in 0.84017 seconds
28 examples, 0 failures
ãªãã»ã©ã
ãå®è£
ã®éäžã§ãªãã¡ã¯ã¿ãªã³ã°ãæ®ã£ãŠããã©ä»æžããåã®è©Šéšã ããšããããå
šéšæžããããã£ãŠãšãã«ãæåŸã«pending
ä»ããŠãããšãæŒãããªããŠè¯ãããããªãã ãããããã®æ©èœãQUnitã«ã欲ããâŠâŠã
è©ŠéšãèŠãŠã¿ã
ã®åã«ã
RSpecã®è©Šéšã®åºæ¬çãªæžãæ¹
èŠãæãããããªé¢šã«æžãã£ãœãã
describe 'What be tested' do it 'will should do' do any.status.should value end end
It will should do
ã®it
ãå·®ããã®ãã€ãŸãè©Šéšã®äž»èªã¯describe
ã«äžãããã®ã§ãããããã
æ¥æ¬èªçã«æžããšããããªïŒ
describe 'èªå販売æ©' do it 'ïŒã«ïŒãéãå ¥ãããšååãåºãŠãã' do åºãŠãããã® = èªå販売æ©.ãéãå ¥ãã(120å) åºãŠãããã®.æ°.㯠1å end end
Controllerã®è©ŠéšãèŠãŠã¿ã
spec/controllers/items_controller_spec.rb
ãªããåé ã«é·ã ãšèª¬æãâŠâŠã
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
ãããšãäœãªã«ïŒ
- ããã¯
scaffold
ã§äœãããspecã§ãã scaffold
ã§çæãããäžéãã®ã³ãŒãã«å¯Ÿå¿ããŠãŸãã- ããŒã§ããã©ã°ã€ã³ãšã䜿ã£ãŠãããåæ Œãããã©ããããããªãã§ãã
- ã³ãŒãã¯RailsãšRSpec-Railsã®APIã ãã䜿ã£ãŠãŸãã
- ïŒä»ã®ã©ã€ãã©ãªãŒã«äŸåããŠããªããšããããšïŒïŒ
- 以åã®ããŒãžã§ã³ãšæ¯ã¹ãŠãã¹ã¿ããã¡ãã»ãŒãžã¯ãããŸã䜿ããªããªããŸããã
ããã§åã£ãŠãïŒ
GET index
ãã«ããŒçãªïŒã¡ãœããã¯é£ã°ããšãæåã®è©Šéšã¯ããã
describe ItemsController do ... describe "GET index" do it "assigns all items as @items" do item = Item.create! valid_attributes get :index, {}, valid_session assigns(:items).should eq([item]) end end ...
ãžãdescribe
ãŠå
¥ãåã«ã§ãããã ãè©Šéšã®ã¿ã€ãã«ã¯å
šéšç¹ãã£ãŠ
ã«ãªã£ãããItemsControllerã®GET indexã¯ãå
šãŠã®é
ç®ãItemsController GET index assigns all items as @items
@items
ã«å²ãåœãŠããã®ã ããã ã
æåã®è¡ã§è©Šéšçšã®ããŒã¿ããitems
ãçæããŠãGET indexã®ãªã¯ãšã¹ããæããŠã¿ãŠãçµæãšããŠ@items
ã«å¯ŸããïŒãã£ãäœã£ãïŒitems
ãšåããã®ãå²ãæ¯ãããŠããã°ãåæ Œãšããäºãã
assigns(:hoge).should
ã§@hoge
ã®äžèº«ãè©Šéšã§ããã£ãŠäºããªïŒãRDocã§assigns
ã£ãŠã¡ãœãããèŠã€ããããªãâŠâŠãRailsã®æ¹ããæ¢ãããåãååã®ããã£ããã©ãããããªïŒ
self.class
ãRSpec::Core::ExampleGroup::Nested_3::Nested_1
ã«ãªã£ãŠããããåããããªå¥ç©ãããªãããããªãã
ãããšãæ°ãåãçŽããŠã
valid_attributes
ãšvalid_session
ã¯{}
ãè¿ãã¡ãœãããšïŒäžã®æ¹ã§ïŒå®çŸ©ãããŠããã
Fixtureã¿ãããªãã®ïŒãè€æ°ããå Žåã¯ã©ããã£ãŠæå®ãããã ããïŒãâŠâŠãšæã£ããã©ãããã¿ããšItem.create!
ãšããã¡ãœãããªã®ã§ãããã¯ãnew
ããŠããsave
ããããšãããã®ãªã®ã§ãããã§äœã£ãŠãããè€æ°äœããªããããè€æ°ããå¿
èŠãããã®ãã
å¥éFixtureã䜿ãããæ¹ãããããã ãã©ããŸã ããããªãã
ããããã£ã¯ãã®ãŸãŸåç §ã§ãã
ãšèšã£ãŠããŸãã®ã«ã¯éåæããããŸããããŸãæ±ãã®ã¯éåæãªãã§ããã¿ããã§ãã
äŸãã°ãæè¿ã®10件ã衚瀺ããªããŠãšãã¯ãããªé¢šã«è©Šéšæžãããããããããªã
it 'assigns latest 10 items as @item' do attr = valid_attributes.clone 1.step(15,1) do |i| attr[:name] = "Item#{i}" Item.create! attr end get :index, {}, valid_session assigns(:items).size.should eq(10) assigns(:items)[0].name.should eq('Item15') end
GET show
ã ãããGET indexãšåãã ãã©ããã©ã¡ãŒã¿ãŒãäžããŠããã
describe "GET show" do it "assigns the requested item as @item" do item = Item.create! valid_attributes get :show, {:id => item.to_param}, valid_session assigns(:item).should eq(item) end end
get
ã®åŒæ°ããã£ãã¯:index
ãš{}
ã ã£ãã®ã:show
ãš{:id => item.to_param}
ã«ãªã£ãŠãããªãã»ã©ç¬¬2åŒæ°ã§ãã©ã¡ãŒã¿ãŒãäžããããã®ãã第3åŒæ°ã¯ãæžããŠããéãã»ãã·ã§ã³ã ãªã
Get new
describe "GET new" do it "assigns a new item as @item" do get :new, {}, valid_session assigns(:item).should be_a_new(Item) end end
eq
ã§ãªãbe_a_new
ãšããããããeq
ã«æžãæããŠã¿ããšãè©Šéšãäžåæ Œã«ãªãã
assigns(:item).should eq(Item.new)
Failures:
1) ItemsController GET new assigns a new item as @item
Failure/Error: assigns(:item).should eq(Item.new)
expected: #<Item id: nil, name: nil, price: nil, description: nil, on_sales: nil, created_at: nil, updated_at: nil>
got: #<Item id: nil, name: nil, price: nil, description: nil, on_sales: nil, created_at: nil, updated_at: nil>
(compared using ==)
ãŸããªããžã§ã¯ãå士ã==
ã§æ¯èŒãããfalse
ã«ãªãã®ã¯åœç¶ã
ããããããã£ãGET showã§eq(item)
ãtrue
ã«ãªã£ãã®ã¯ïŒïŒ
describe "GET show" do it "assigns the requested item as @item" do item = Item.create! valid_attributes get :show, {:id => item.to_param}, valid_session assigns(:item).should eq(item) item2 = Item.new item2.id = item.id assigns(:item).should eq(item2) end end
ãªãã»ã©ãIDãããå Žåã¯IDãåèŽãããã§ç¢ºèªããŠãã®ããIDãnil
ã®å Žåã¯ããããªããããbe_a_new
ã§ç¢ºèªããå¿
èŠãããããšã
GET edit
ç¹ã«èŠæãªããGET showãšããäºã¯äžç·ã ãã®ãã
POST create
ãããããã¯ãªãããããããæãã«ãªã£ãŠãã
describe "POST create" do describe "with valid params" do it "creates a new Item" do expect { post :create, {:item => valid_attributes}, valid_session }.to change(Item, :count).by(1) end it "assigns a newly created item as @item" do post :create, {:item => valid_attributes}, valid_session assigns(:item).should be_a(Item) assigns(:item).should be_persisted end it "redirects to the created item" do post :create, {:item => valid_attributes}, valid_session response.should redirect_to(Item.last) end end describe "with invalid params" do it "assigns a newly created but unsaved item as @item" do # Trigger the behavior that occurs when invalid params are submitted Item.any_instance.stub(:save).and_return(false) post :create, {:item => {}}, valid_session assigns(:item).should be_a_new(Item) end it "re-renders the 'new' template" do # Trigger the behavior that occurs when invalid params are submitted Item.any_instance.stub(:save).and_return(false) post :create, {:item => {}}, valid_session response.should render_template("new") end end end
é ã«èŠãŠè¡ããããã
DBã«è¿œå
describe "with valid params" do it "creates a new Item" do expect { post :create, {:item => valid_attributes}, valid_session }.to change(Item, :count).by(1) end
post
ãããšItem
ã®æ°ã1å¢ããããšããå
容ã ãšæããby
ã¯ãããã€ã«ãªãããã§ã¯ãªããããã€å¢ãããããæžããã ããå
ã«Item.create
ãäœåºŠãã£ãŠãããšããŠããby(1)
ãããšexpect
å
ã§ããã€å¢ããããã確èªããŠãã
it "creates a new Item" do Item.create! valid_attributes Item.create! valid_attributes expect { Item.create! valid_attributes post :create, {:item => valid_attributes}, valid_session }.to change(Item, :count).by(2) end
ã¯ã次ã
ãªããžã§ã¯ãäœæ
it "assigns a newly created item as @item" do post :create, {:item => valid_attributes}, valid_session assigns(:item).should be_a(Item) assigns(:item).should be_persisted end
be_a
ãšbe_persisted
ãååºãªãããããbe_persisted
ãŠäœïŒãpersistã§ãïŒæ¶ããã«ïŒåšãããŠæå³ã ãããnil
ãããªããã£ãŠç¢ºèªïŒãã§ããããªãbe_a(Item)
ãéããªãç¡æå³ã§ã¯ïŒ
ã©ããããDBã«åšãããšãã確èªã§ãããããã
be_xxx
be_xxx
ã§obj.xxx?
ã«çžåœãããããã
ã€ãŸãbe_persisted
ã¯ActiveRecord::Persistence – persisted?()ãå®è¡ããã
䌌ããã®ã§ãhas_xxx?
ãå®è¡ããhave_xxx
ãšããã®ããããããã
ãªãã€ã¬ã¯ã
it "redirects to the created item" do post :create, {:item => valid_attributes}, valid_session response.should redirect_to(Item.last) end
èªãã°ãããã
ãããŸã§ãwith valid params
ãªãšãã«ã€ããŠã®descriptionã
ããããã¯äžæ£ãªå ¥åã«ã€ããŠã§ãã
describe "with invalid params" do
äžæ£ãªå
¥åã®ãšãã¯ãDBã«ä¿åãããŠããªã@item
ãæ°ããäœã
it "assigns a newly created but unsaved item as @item" do # Trigger the behavior that occurs when invalid params are submitted Item.any_instance.stub(:save).and_return(false) post :create, {:item => {}}, valid_session assigns(:item).should be_a_new(Item) end
è¬ã®åªæItem.any_instance.stub(:save).and_return(false)
ãæãã§ããä»ã¯è¯ãããªã
èªããšãªããšãªããããããã£ãšãItemã®ã©ã®ã€ã³ã¹ã¿ã³ã¹ã§ããsave
ã¡ãœããã¯false
ãè¿ãã¹ã¿ãã«çœ®ãæãããã£ãŠãªæå³ãªãã ããã
ã§ãsave
ã倱æãããšãã«@item
ãã¡ãããšäœãããŠããäºã確èªãããã©ããªãšãã«å€±æããããã¯ControllerãããªããŠModelã®ç®¡çäžã ãªã
{:item => {}}
ã¯ããããããªãã{}
ã§ãããããããªãã®ïŒãå®éã«ããã§ãè©Šéšã¯åæ Œã«ãªãããããšã{:item => { name: 'MyName' }}
ã«ããŠassigns(:item).name.should eq('MyName')
ã¿ããã«ããããã«çšæããŠãããã ãããã
äžæ£ãªå ¥åã®ãšãã¯ãnewã®ãã³ãã¬ãŒãã§åºåããã
it "re-renders the 'new' template" do # Trigger the behavior that occurs when invalid params are submitted Item.any_instance.stub(:save).and_return(false) post :create, {:item => {}}, valid_session response.should render_template("new") end
ã¢ã¯ã·ã§ã³ãšå¯Ÿå¿ããªããã³ãã¬ãŒããåºåãããšãã¯render_template
ã§ç¢ºèªããšããµããµãã
ããã§POST createã®è©Šéšã¯ãããããµããŒã
POT update
ããããŸãé·ãã
describe "PUT update" do describe "with valid params" do it "updates the requested item" do item = Item.create! valid_attributes # Assuming there are no other items in the database, this # specifies that the Item created on the previous line # receives the :update_attributes message with whatever params are # submitted in the request. Item.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) put :update, {:id => item.to_param, :item => {'these' => 'params'}}, valid_session end it "assigns the requested item as @item" do item = Item.create! valid_attributes put :update, {:id => item.to_param, :item => valid_attributes}, valid_session assigns(:item).should eq(item) end it "redirects to the item" do item = Item.create! valid_attributes put :update, {:id => item.to_param, :item => valid_attributes}, valid_session response.should redirect_to(item) end end describe "with invalid params" do it "assigns the item as @item" do item = Item.create! valid_attributes # Trigger the behavior that occurs when invalid params are submitted Item.any_instance.stub(:save).and_return(false) put :update, {:id => item.to_param, :item => {}}, valid_session assigns(:item).should eq(item) end it "re-renders the 'edit' template" do item = Item.create! valid_attributes # Trigger the behavior that occurs when invalid params are submitted Item.any_instance.stub(:save).and_return(false) put :update, {:id => item.to_param, :item => {}}, valid_session response.should render_template("edit") end end end
æŽæ°æå
ãªããã³ã¡ã³ããã
# Assuming there are no other items in the database, this
# specifies that the Item created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
ãããšïŒ
- DBã«ã¯ä»ã®é
ç®ããªããã®ãšããŠãäžã®è¡ã§äœã£ã
Item
ãªããžã§ã¯ãã¯ã©ããªãã©ã¡ãŒã¿ãŒïŒãªã¯ãšã¹ãã§é£ãã§ãããã€ïŒã§ã:update_attributes
ã§åãåãããšããäºã確èªããŸãã
DBã«ä»ã®é ç®ããã£ãŠãããããããªãã®ïŒ
ãŸãããã¯çœ®ããŠãããŠããšãããã©ããªãã©ã¡ãŒã¿ãŒã§ã:update_attributes
ãšããååã®ã¡ãã»ãŒãžïŒOOPçãªè¡šçŸã ãã©ãè©°ãŸãæã¡ãœããïŒã§åãåãããšãthese
ãªããŠã«ã©ã ã¯å®çŸ©ããŠãªãããã©ãåãåãã ãåãåã£ãŠãŸãããšããäºããªã
ããã§ç¢ºèªããã®ã¯ãDBãæŽæ°ããã¡ãœãããåŒã¶ãšãããŸã§ãå®éã«DBãæŽæ°ãããäºã®ç¢ºèªã¯Modelã®è©Šéšã§ã
ãªã¯ãšã¹ãéãã®@item
ãäœæ
it "assigns the requested item as @item" do item = Item.create! valid_attributes put :update, {:id => item.to_param, :item => valid_attributes}, valid_session assigns(:item).should eq(item) end
GET showãšæŠãåããã
ãªãã€ã¬ã¯ã
ç¹ã«èŠããšããã¯ãªãã£ãããã£ãã
äžæ£ãªå
¥åã®ãšãã§ãã@item
ãäœããã
ç¹ã«èŠããšããã¯ãªãã£ãããã£ããã£ãã
äžæ£ãªå ¥åã®ãšãã¯ãeditã®ãã³ãã¬ãŒãã§åºåããã
ç¹ã«ãã£ãã
DELETE destroy
ã²ããŒãã£ãšControllerã®æåŸã ã
<
div>
describe "DELETE destroy" do it "destroys the requested item" do item = Item.create! valid_attributes expect { delete :destroy, {:id => item.to_param}, valid_session }.to change(Item, :count).by(-1) end</p> <pre><code>it &quot;redirects to the items list&quot; do item = Item.create! valid_attributes delete :destroy, {:id =&gt; item.to_param}, valid_session response.should redirect_to(items_url) end </code></pre> <p>end
ãããŸã§èŠãŠãããšãããããžããŒãªãã»ã©ããŠãªæãã
ãã€ããããŸã§ãããå š28åäžã16åã®examplesãControllerã«ãããšããäºãã
ãªã¯ãšã¹ãã®è©Šéš
Named routesã®è©Šéšãšããäºãªã®ããªïŒ
spec/requests/items_spec.rb
describe "Items" do describe "GET /items" do it "works! (now write some real specs)" do # Run the generator again with the --webrat flag if you want to use webrat methods/matchers get items_path response.status.should be(200) end end end
items_path
ãget
ãããHTTPã®ã¹ããŒã¿ã¹ã³ãŒãã200
ã«ãªãããªãã ãã©ããã®ãã¹ã§ãªã¯ãšã¹ããæãããitems#index
ãå®è¡ããäºã確ãããªãã¡ããããªããããªãããããã¯ã«ãŒãã£ã³ã°ã®è©Šéšã ããå¥ïŒæ¬¡é
ïŒãã
items_path
ã/items
ãè¿ãäºã¯ã©ããã£ãŠç¢ºèªãããã ããïŒ
ã«ãŒãã£ã³ã°ã®è©Šéš
spec/routing/items_routing_spec.rb
describe ItemsController do describe "routing" do it "routes to #index" do get("/items").should route_to("items#index") end it "routes to #new" do get("/items/new").should route_to("items#new") end it "routes to #show" do get("/items/1").should route_to("items#show", :id => "1") end it "routes to #edit" do get("/items/1/edit").should route_to("items#edit", :id => "1") end it "routes to #create" do post("/items").should route_to("items#create") end it "routes to #update" do put("/items/1").should route_to("items#update", :id => "1") end it "routes to #destroy" do delete("/items/1").should route_to("items#destroy", :id => "1") end end end
route_to
ã ã£ãŠã
ããã ãèŠããšé¿åã£ãœããã©ïŒresource
ã®è©Šéšã¿ããã ãïŒãroutes.rb
ãæ¬æ°ã§ãããã ããã䟿å©ããã
ãã£ã¡ã¯ãªã¯ãšã¹ãã®è©Šéšãšéã£ãŠãitems_path
ãããªããŠ`”/items”ã¿ããã«ãã¹ãçŽæ¥æžãããŠãããã ãã©ãã©ããã䜿ãåããªãã ãããâŠâŠã
indexã®View
describe "items/index" do before(:each) do assign(:items, [ stub_model(Item, :name => "Name", :price => 1, :description => "MyText", :on_sales => false ), stub_model(Item, :name => "Name", :price => 1, :description => "MyText", :on_sales => false ) ]) end it "renders a list of items" do render # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "tr>td", :text => "Name".to_s, :count => 2 # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "tr>td", :text => 1.to_s, :count => 2 # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "tr>td", :text => "MyText".to_s, :count => 2 # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "tr>td", :text => false.to_s, :count => 2 end end
before
ãšassign
ãšstub_model
ãç®æ°ããæãã
before
ã ã£ãŠãåŒæ°ã«it
ã«äžããååïŒãããªã"renders a list of items"
ïŒãäžããäºã§åå¥ã«æå®ã§ããã¿ãããããšåãããã«after
ãšãããã®ããã£ãã
ã§ããã£ãŠãåŠçã¯assign
ããã£ãåºãŠããassigns
ã¯ã€ã³ã¹ã¿ã³ã¹å€æ°ãåã£ãŠãããã©ãããã®ã¯éã«å€æ°ã«å€ãäžããäºãã§ããã¿ããã
stub_model
ã¯ãã®åã®éãã¢ãã«ã®ã¹ã¿ããäœãã¡ãœãããã
ã«ã©ã åã«ãããã·ã³ãã«ã¯ãå
ã®ã¢ãã«ã«çžãããªãã¿ãããé©åœãªååã§å€ãæž¡ããŠããéãããŸãçç¥ããå Žåãå
ã
ãããã®ã¯åºåããããïŒ:name =>
ã:hoge =>
ã«ãããšãViewã§ã¯name
ãhoge
ã䜿ãããïŒããã®å Žåã代å
¥ãããŠããªãã«ã©ã ã¯nil
ã«ãªã£ãŠãã£ãœãã
render
ã¯HTMLãåºåãããã€ã§ãããformat.json
ãšããHTML以å€ã®ãšãã¯ã©ãããã®ããªãã
assert_select
ããã¯Railsã®æ¹ã®ã¡ãœããã¿ããã
- ActionDispatch::Assertions::SelectorAssertions – assert_select(*args, &block)
- assert_selectã§HTMLãæ€èšŒãã – Rails RecipeBookã¯ãŠãªç – rails-recipebookã°ã«ãŒã
:count
ã¯ç¢ºèªé
ç®(assertion)ã ãã©:text
ã®æ¹ã¯çµã蟌ã¿ã®æ¡ä»¶(narrowing)ã§ãããããã*args
ã¯ãããªæããã
*args := [container_element,] selector [, condition [, ...]]
selector
ã¯container_element
èªäœã«ãæå¹ã§ããããããã€ãŸãE
å
ã§.c
ãšãããE.c
ãšE .c
ãæ¢ãã
:text
ã«ã¯æ£èŠè¡šçŸã䜿ãããããã
ã§ãå
¥ãåã«åºæ¥ããäŸãã°ãã©ãŒã ã«äœ¿ãlabel
èŠçŽ ã«ã¯å
šãŠhoge
ã¯ã©ã¹ãäžããããŠããããšãã確èªãèããã
it 'renders all labels with `hoge` class' do assert_select 'form#myform label' do assert_select '.hoge' end end
label
èŠçŽ ãå
šéšæ¢ããŠããŠãããããã«ã€ããŠ.hoge
ã«ãããããäºã確èªãããšã
æ°ãããã£ãŠããã°label.hoge
ã«:count
ã§ãè¯ãããã ãã©ãã§ãããã ãš.hoge
ãäžããããŠããªãlabel
ããããšå°ãã
ãããšãããããããã§ã
assert_select "tr>td", :text => "Name".to_s, :count => 2
ããâã¯ã
- ã»ã¬ã¯ã¿ãŒ
tr>td
ã«ããããã - ãã€å
容ã®æååã
"Name"
ã§ãããã®ãã - 2åãã
âãšãã確èªã§ãã
webratïŒïŒ
# Run the generator again with the --webrat flag if you want to use webrat matchers
ãªããCucumberã£ãŠã®ãšçµã¿åãããŠãè¯ãæãã«è©Šéšé ç®ãèšè¿°ã§ããããŒã«ãããã
ã·ããªãª: æ°ãããšã³ããªã®ç»é² åæ ãšã³ããªæ°èŠäœæããŒãžã衚瀺ããŠãã ã〠"ã¿ã€ãã«"ã«"Webratãã¹ãŽã(ç¶:Cucumberãã¢ãã)"ãšå ¥åãã ã〠"æ¬æ"ã«"Railsã¢ããªã®ãã¹ããé«ãæœè±¡åºŠã§ïœ¥ïœ¥ïœ¥"ãšå ¥åããã ã〠"Create"ãã¿ã³ãã¯ãªãã¯ãã ãªãã° "Webratãã¹ãŽã"ãšè¡šç€ºãããŠããããš
ããããããããããªã«ããããããã°ãè¶ äŸ¿å©ãããïŒããããªãããã°ã©ãã³ã°ãå šãã§ããªã人ã§ãç解ã§ãããïŒããã°ã©ãã³ã°ã§ãå¿ èŠãªè«çæèåã¯å¿ èŠããã ãã©ãïŒã次ã¯ããèŠãŠã¿ãªããšã
ã§ã次ããRSpecèŠçµãã£ãŠãããã
showã®View
describe "items/show" do before(:each) do @item = assign(:item, stub_model(Item, :name => "Name", :price => 1, :description => "MyText", :on_sales => false )) end it "renders attributes in <p>" do render # Run the generator again with the --webrat flag if you want to use webrat matchers rendered.should match(/Name/) # Run the generator again with the --webrat flag if you want to use webrat matchers rendered.should match(/1/) # Run the generator again with the --webrat flag if you want to use webrat matchers rendered.should match(/MyText/) # Run the generator again with the --webrat flag if you want to use webrat matchers rendered.should match(/false/) end end
rendered
ã¯String
ã§ãåºåãããHTMLããŸãããšå
¥ã£ãŠãããã§ãããã ãã©ãŠã¶ãŒã«éä¿¡ãããHTMLã§ã¯ãªããã®Viewãæ
åœããŠããéšåã ãã
editã®View
describe "items/edit" do before(:each) do @item = assign(:item, stub_model(Item, :name => "MyString", :price => 1, :description => "MyText", :on_sales => false )) end it "renders the edit item form" do render # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "form", :action => items_path(@item), :method => "post" do assert_select "input#item_name", :name => "item[name]" assert_select "input#item_price", :name => "item[price]" assert_select "textarea#item_description", :name => "item[description]" assert_select "input#item_on_sales", :name => "item[on_sales]" end end end
ããassert_select
ã掻çšããŠãã:action
, :method
, :name
ãšãããã®ãããã®ããããHTMLã®èŠçŽ ãæå®ã§ããã ãïŒ
ããããinput
ã®value
ã確èªããã®ã«ããããã®ã¯ã©ãã ïŒ
assert_select "input#item_name", :name => "item[name]", :value => 'hoge'
åœç¶FailureããªãäºãæåŸ ãããã ãããªããªãã£ãããããã
assert_select "input#item_name", :name => "item[name]" do assert_select "[value=#{@item.name}]" end
ãã£ã¡ã®æžãæ¹ãªã䜿ãããïŒã»ã¬ã¯ã¿ãŒã¯ãã¡ãããŸãšããŠãåãçµæã«ãªããã ãã©ãåããæ¹ãèªã¿ãããããªãšãïŒïŒããšãæ¬åœã¯ãšã¹ã±ãŒãããªããšã»ã¬ã¯ã¿ãŒã厩å£ããããïŒãããããå ¥åå€ã確èªããæ¹æ³ã¯å¥éçšæãããŠããã®ããªã
newã®View
ã»ãšãã©editãšåãã
éãã®ã¯è©Šéšçšã®@item
ãäœæãããViewã®æ¹ã§@item
ã«å²ãåœãŠããããã®ã«as_new_record
ããããšãããããã
describe "items/new" do before(:each) do assign(:item, stub_model(Item, :name => "MyString", :price => 1, :description => "MyText", :on_sales => false ).as_new_record) end
æ°èŠäœæãªãã ãããã€ã³ã¹ã¿ã³ã¹å€æ°ã®å€ã¯è»äžŠã¿nil
ã§ããã¹ããªããããªããïŒ
ãããšãããã§äžéãèŠãããªã
ãŸãšã
- ãã£ãã䟿å©ããã
- æžåŒã¯è±èªçã«èªã¿ãããããã¶ããããã°ã俺ïŒæ¥æ¬äººïŒã
- ãªãã¡ã¬ã³ã¹çãªãã®ãèŠã¥ããããšãããã©ããã«ããã®ïŒïŒ
- å ¬åŒãµã€ãã«Documentationãšããäºã§RDocã«ãªã³ã¯ããããã©ãããã«èŒã£ãŠãªãã®ãããâŠâŠã
- Controllerã®è©Šéšã®
be_xxx
ãªããã¯èªåçæãœããããææžã«ã¯æªæ²èŒãªãã ãããšæãã - RSpecã®ã¡ãœãããªã®ãRailsã®ã¡ãœãããªã®ãããããããŠRubyæšæºã®ã¡ãœãããªã®ãåºå¥ãä»ããªããïŒåã足ããªãïŒ
- Railsã«éããRubyã§äŸ¿å©ããã
- ã§ã
ãŠèšãããã (ÂŽã»Ïã»ïœ)-bash: rspec: command not found
- ã§ã
- webratãããããæ©ãè©Šãããã
- Named routesã®è©Šéšæ¹æ³ã¯ããããããªãã
ãšã£ãŽãã±ããã®ã·ãã