我一直在尝试测试/存根调用 twitter api 的应用程序方法方法,但我没有取得任何成功,因为该方法仍在调用原始方法。我错过了什么,因为我一直在使用 cucumber 和 fakeweb ......现在我我正在使用 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