Twitter api を呼び出すアプリケーション メソッド メソッドをテスト/スタブしようとしていますが、メソッドがまだ元のメソッドを呼び出しているため、成功していません。 Webmock を使用していますが、まだリクエストをスタブしていません。
私の宝石ファイル
source 'https://rubygems.org'
gem 'rails', '3.2.13'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
gem 'twitter'
#### gem 'grackle'
###gem 'rack-ssl'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'simplecov', :require => false, :group => :test
group :test do
gem 'cucumber-rails', :require => false
# database_cleaner is not required, but highly recommended
gem 'database_cleaner'
gem 'webmock'
gem "rspec-rails", "~> 2.4"
end
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
#basic configuration using the credentials
def get_tweets search_text
begin
# hit the API
TWITTER_CLIENT.client.search(search_text, :count => 10, :result_type => "recent").collect do |tweet|
@tweets = "#{tweet.urls}"
end
rescue
@tweets = "It seems that something has went wrong,Please try after some time" if @tweets == nil
end
end
end
私の機能ファイル
Feature: Verify tweet page
In order to search for a hashtag
the user will enter the details in the textbox and click on submit button
Scenario: Land on tweet page and search for a name and verify
Given a user visits the tweet page
Then the user should see "Search"
##user fill in Bill
When the user fill in the textbox
##And the user clicks the Search button
Given the remote service will return a valid response
Then the user should see "http://some1.url.test"
Then the user should see "http://some2.url.test"
Then the user should see "http://some3.url.test"
私の web_steps.rb
コメントしたコードは試されましたが、本文を印刷すると、「何か問題が発生したようです。しばらくしてから試してください」というエラーメッセージが表示されます
Given /^the remote service will return a valid response$/ do
##static file having json response containing the url that i am looking in feature file
@canned_response = File.open("#{Rails.root}/test/responses/canned_response.json").read
controller=ApplicationController.new
stub_request(:get, "http://localhost:4567/tweet/user_tweet").to_return(canned_response)
###puts Net::HTTP.get("www.example.com", '/')
#WebMock.stub_request(:any, "http://localhost:4567").to_return(canned_response)
# uri = URI.parse("http://www.example.com/")
# req = Net::HTTP::Post.new(uri.path)
# req['Content-Length'] = 3
# Net::HTTP.start(uri.host, uri.port) {|http|
# http.request(req, "abc")
# }
# p req
# canned_response = IO.read("#{Rails.root}/test/responses/canned_response.json")
# {:body => canned_response, :status => 200}
# stub_request(:any, 'http://localhost:4567').to_return do |request|
# # p '22222222222222'
# body = CGI::unescape(request.body)
# # p '333333333333333'
# code = body.match(/<CODE_NBR>(.*)<\/CODE_NBR>/)
# # p "hiiiiiiiiiiiiiiiiii"
# # p code
# response = canned_response
# {:body => response, :status => 200}
# end
# p body
end