�O���Rails�Ŏg����e�X�g�t���[�����[�N�����Љ�܂����B����͋�̓I��Web�A�v�����ɁA�ȒP�ȃe�X�g���g�������t�@�N�^�����O�ɂ��ĉ�����܂�
�@�O���́ARails�Ŏg����e�X�g�t���[�����[�N�����Љ�܂����B����A���悢����ۂ̃e�X�g�������܂��B�������������ł͕�����Ȃ��̂ŁA�O�X��̘A�ڂŎw�E�����R�[�h���r���[�̌�������A���t�@�N�^�����O�̌������X�g�A�b�v���A�e�X�g�������Ȃ���1��1�Ԃ��Ă����܂��傤�B
�@�܂���1�́A�ubitly�̐ݒ�v�̃��W�b�N��ύX�������Ǝv���܂��B���̕����ł��B
class ItemsController < ApplicationController conf = APP_CONFIG["bitly"] @@bitly = Bitly.new(conf["username"], conf["apikey"])
�@�����A���ʂɃ��t�@�N�^�����O�ɂ���ăR�[�h��ύX���Ă��A�����Ƌ@�\���邩�ǂ������s���Ȃ̂ŁA��ɃC���e�O���[�V�����e�X�g��ݒ肵�܂��傤�B�����ł�Rails�J���҂̊ԂŐl�C�̍���Cucumber�i�L���[�J���o�[�j�𗘗p���܂��B
�@Cucumber�̐ݒ��cucumber-rails���g���ƊȒP�ɂł���悤�ł��B�܂�Gemfile�Ɉȉ���lj����܂��B
group :test do �@gem "rspec-rails" gem 'cucumber-rails' gem 'capybara' gem 'database_cleaner' end
�@�����āubundle install�v�Ƃ���gem���C���X�g�[��������ɁA�ȉ��̂悤��rspec��cucumber�����ꂼ��C���X�g�[�����܂��B
rails g rspec:install rails g cucumber:install --rspec --capybara
�@���̌��generator���g���ƁA�ȉ��̂悤�ȁufeature�v�̐��`������܂��B
ruby script/rails generate cucumber:feature Feature: Manage items In order to [goal] [stakeholder] wants [behaviour] Scenario: Register new item Given I am on the new item page And I press "Create"
�@�ł́A���ۂ�feature�������Ă݂܂��傤�Bfeature�Ƃ����̂͋@�\�̂��ƂŁA�ǂ������@�\�������āA�N�������ł��邩�����R����i�����ł͉p��ł����A���{����g���܂��j�ŋL�q���܂��B�ŏ��ɁA���O�C����Ԃł����item���lj��o�^�ł���Ƃ���feature�̋L�q��ł��B
Feature: Manage items In order to register new item As a user I want to add new item Scenario: Register new item Given I am logged in as "Bob" And I am on the root page And I follow "Users" And I follow "Bob" And I fill in "http://www.google.com" for "new_item" And I press "Add" Then I should see "Created an item"
�@���̏�Ԃ�cucumber�𑖂点��ƁA�ȉ��̃X�e�b�v�����K�v������ƌx�����łĂ��܂��B
[worklista (29df2bb...)]$ rake cucumber 1 scenario (1 undefined) 7 steps (5 skipped, 2 undefined) 0m0.026s You can implement step definitions for undefined steps with these snippets: Given /^I am logged in as "([^"]*)"$/ do |arg1| pending # express the regexp above with the code you wish you had end
�@�t�Ɍ����Ƃ���ȊO�̃X�e�b�v�͂��łɑ��݂���Ƃ������Ƃł��B���́urails g cucumber:install�v�Ƃ����Ƃ��ɁA�ȉ��̃t�@�C��������Ă��܂��B
features/step_definitions/web_steps.rb features/support/env.rb features/support/paths.rb lib/tasks/cucumber.rake script/cucumber
�@���̒��́uweb_steps.rb�v�t�@�C����`���Ă݂�ƈȉ��̂悤�ȃX�e�b�v������Ă��܂��B
module WithinHelpers def with_scope(locator) locator ? within(locator) { yield } : yield end end World(WithinHelpers) Given /^(?:|I )am on (.+)$/ do |page_name| visit path_to(page_name) end When /^(?:|I )go to (.+)$/ do |page_name| visit path_to(page_name) end When /^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/ do |button, selector| with_scope(selector) do click_button(button) end end When /^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/ do |link, selector| with_scope(selector) do click_link(link) end end
�@�ł́A����`�̃X�e�b�v��features/step_definitions.rb�ɒlj����܂��B���[�U�[�����O�C������t���[���L�q���܂��B�ȉ��̒ʂ�ł��B
Given /^I am logged in as "([^"]*)"$/ do |arg1| Given %{I am on the home page} And %{I follow "Sign up"} And %{I fill in "[email protected]" for "user_email"} And %{I fill in "Bob" for "user_username"} And %{I fill in "testforbob" for "user_password"} And %{I fill in "testforbob" for "user_password_confirmation"} And %{I fill in "dummy_code" for "user_invite_code"} And %{I press "Sign up"} And %{I follow "Login"} And %{I fill in "[email protected]" for "user_email"} And %{I fill in "testforbob" for "user_password"} And %{I press "Sign in"} end
�@���O�C���̏ڍׂ͍����feature�ł͂��܂�d�v�����Ă��Ȃ������Ȃ̂ŁA�����ł�1�̃X�e�b�v�ɂ܂Ƃ߂Ă݂܂����B
�@���āA����ňȉ��̂悤�ɁA������feature���p�X����͂��ł��B
[worklista (1cc509a...)]$ rake cucumber [2011-01-03 21:35:51] INFO WEBrick 1.3.1 [2011-01-03 21:35:51] INFO ruby 1.9.2 (2010-08-18) [x86_64-darwin10.4.0] [2011-01-03 21:35:51] INFO WEBrick::HTTPServer#start: pid=26542 port=9887 1 scenario (1 passed) 8 steps (8 passed) 0m15.348s
�@���͍���̃R�~�b�g�ł�features/support/env.rb�Ɉȉ��̂悤�Ȑݒ���t�������Ă����܂����B
Capybara.javascript_driver = :selenium
�@Cucumber�́A�ʏ��Webrat�Ƃ����u���E�U�̓������V�~�����[�g���郉�C�u�������g���܂��B�������Ȃ���V�~�����[�^�ł�JavaScript�̓���Ȃǂ��e�X�g���邱�Ƃ��ł��܂���B������Capybara�Ƃ������C�u�������g���܂��BCapybara���g���ƁA���ۂɃu���E�U�𗧂��グ�Ď����e�X�g���邽�߂̂��܂��܂ȃc�[���ƊȒP�ɓ������邱�Ƃ��ł��܂��B�����ł�Selenium�Ƃ����c�[�����g���Ă��܂��B�������邱�ƂŁu@javascript�v���t����feature���A���ۂɃu���E�U��Ńe�X�g���邱�Ƃ��ł���悤�ɂȂ�܂��B
�@����̓u���E�U�Ƃ���Firefox���g���Ă��܂����A���̃u���E�U�ɐ�ւ��邱�ƂŃN���X�u���E�U�e�X�g���\�ł��B���ۂ̃u���E�U���g���������e�X�g�̓V�~�����[�g�ł��x���̂ŁA�K�v�ȃe�X�g�ɂ����g�����ق����ǂ��ł����A���ۂɃR�[�h�������Ȃ��X�e�[�N�z���_�[�i���ۂɃV�X�e�����g���G���h���[�U�[��v���W�F�N�g�ɏo�����Ă���r�W�l�X�I�[�i�[�Ȃǁj��A�v���_�N�g�I�[�i�[�Ɏ������e�X�g�̈З͂��f���������Ƃ��ɂ́ASlenium�̂悤�Ƀu���E�U�Ŏ����œ������̂�������ƌ��\�C���p�N�g������̂ł����߂ł��B
�@�ʏ�̃e�X�g�쓮�J���ł́u���b�h�A�O���[���A���t�@�N�^�[�v�̃��Y���ōs���̂��ǂ��ƌ����܂��B���b�h�̓e�X�g�P�[�X�����s���Ă��邱�Ƃ������A�O���[���̓e�X�g���p�X�������Ƃ������܂��B
�@�e�X�g�쓮�J���ł́A�ŏ��Ƀe�X�g�������A���̃e�X�g�����s����̂��m�F���܂��i���b�h�j�B���ɁA���̃e�X�g���p�X�����邽�߂̍Œ���̃R�[�h�������܂��i�O���[���j�B�Ō�ɁA�����ꂽ�R�[�h�����t�@�N�^�����O���邱�ƂŁu�@�\�lj��v�Ɓu�R�[�h�����ꂢ�ɂ����Ɓv���A����ǂ��čs���܂��B
�@�����̃R�[�h�Ɍォ��e�X�g��lj�����ꍇ�A���̎�@�͈ꌩ�g���Ȃ��悤�Ɏv���܂����A���Ƃ���������͂���܂��B��قǂ̃e�X�g���O���[���Ȃ̂��m�F������A���t�@�N�^�����O����\��̉ӏ�����x�폜���Ă݂܂��傤�B
[worklista (8540c64...)]$ git co 8540c64f2a7ae4f0d4e7fdd3494bed7cce2ca4c2 - conf = APP_CONFIG["bitly"] - @@bitly = Bitly.new(conf["username"], conf["apikey"])
�@���̏�ԂŃe�X�g�𑖂点��ƁA������b�h�i���s�j�ɂȂ�܂���ˁB
[worklista (refactoring)]$ rake cucumber (::) failed steps (::) uninitialized class variable @@bitly in ItemsController (NameError) ./app/controllers/items_controller.rb:111:in `populate_retweet' ./app/controllers/items_controller.rb:89:in `populate' ./app/controllers/items_controller.rb:31:in `create' :10:in `synchronize' ./features/step_definitions/web_steps.rb:29:in `block (2 levels) in ' ./features/step_definitions/web_steps.rb:14:in `with_scope' ./features/step_definitions/web_steps.rb:28:in `/^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/' features/manage_items.feature:12:in `And I press "Add"' Failing Scenarios: cucumber features/manage_items.feature:6 # Scenario: Register new item 1 scenario (1 failed) 8 steps (1 failed, 2 skipped, 5 passed)
�@���̏�ԂŁA���x��bitly�̐ݒ��config.rb�Ɉڍs���܂��B
[worklista (2012b30...)]$ git show 2012b30d483d17cb89978c8f200895089445d180 commit 2012b30d483d17cb89978c8f200895089445d180 Author: Makoto Inoue <[email protected]> Date: Sun Dec 26 22:49:19 2010 +0000 Moved Bitly initializer to config.rb diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index a002af4..3312722 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -5,7 +5,6 @@ require 'resolv-replace' class ItemsController < ApplicationController before_filter :authorise_as_owner - def create @user = User.find(params[:user_id]) @@ -108,7 +107,7 @@ private end def populate_retweet(item) - url = @@bitly.shorten(item.url) + url = BITLY.shorten(item.url) item.bitly_url = url.short_url item.retweet = url.global_clicks end diff --git a/config/initializers/config.rb b/config/initializers/config.rb new file mode 100644 index 0000000..d740917 --- /dev/null +++ b/config/initializers/config.rb @@ -0,0 +1,6 @@ +APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env] + +require 'bitly' +Bitly.use_api_version_3 +conf = APP_CONFIG["bitly"] +BITLY = Bitly.new(conf["username"], conf["apikey"])
�@config.rb�̒��ł�ItemsController�̃N���X�ϐ��i@@bitly�j�͐ݒ�ł��Ȃ��̂ŁA�R���X�^���g�iBITLY�j�ɕς��Ă݂܂����B
�@�ł͂��̏�Ԃł�����x�e�X�g�𑖂点�Ă݂܂��傤�B
git co 2012b30d483d17cb89978c8f200895089445d180 [worklista (2012b30...)]$ rake cucumber ........ 1 scenario (1 passed) 8 steps (8 passed)
�@�����ƃO���[�����ł܂����ˁB
�@���̂悤�Ɂu�O���[���A���b�h�A�O���[���A���t�@�N�^�[�v�̎菇�ނ��ƂŁA���ݕύX���Ă���ӏ����J�o�[����e�X�g������̂������Ɗm�F���Ȃ��烊�t�@�N�^�[�����邱�Ƃ��ł��܂����B
�@����̓R���g���[���̃e�X�g�Ɉڂ��Ă����܂��B
Copyright © ITmedia, Inc. All Rights Reserved.