ããããã
ãã®èšäºã¯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ã®è©Šé𿹿³ã¯ããããããªãã
ãšã£ãŽãã±ããã®ã·ãã