WebMock:Rspec - 使用JSON响应测试Facebook验证 [英] WebMock: Rspec - Test Facebook Validation by using JSON response
问题描述
我很难为我的程序编写规格,因为我需要在填写所有正确的格式信息后验证facebook ID。我的Facebook ID是从JSON响应中检索的,所以我不确定如何在我的规范中描述此内容。我对这个领域非常陌生,在这一点上我一直陷入困境。如果有一些示例代码可以帮助我,我将不胜感激。以下是我的一半完成的规格。
I am having difficulty to write specs for my program as I need to validate the facebook ID after I filled in all the correct format information. My facebook ID is retrieved from JSON response so I not sure how do I portray this in my specs. I am very new to this field and I had been stuck at this point for a long time. I will be grateful if there are some sample codes to help me in this. Below are my half-done specs.
场景:使用给定Fbid的用户通过使用 http://graph.facebook.com (JSON响应)匹配如果用户名和fbid相同。
Scenario: Use user given Fbid to validate facebook page by using "http://graph.facebook.com" (JSON Response) to match if username and fbid are the same.
我的github网址: https://github.com/amycheong/company_list
My github url : https://github.com/amycheong/company_list
更新:我曾经使用过WebMock,但最终得到:
Updated: I had used WebMock but end up getting:
1) Companies new company create with valid information correct facebook id should validate fbid
Failure/Error: Company.validate_fbid('pepsi')['username'].should == "pepsi"
WebMock::NetConnectNotAllowedError:
Real HTTP connections are disabled. Unregistered request: GET http://graph.facebook.com:443/pepsi with headers {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}
You can stub this request with the following snippet:
stub_request(:get, "http://graph.facebook.com:443/pepsi").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})
registered request stubs:
stub_request(:get, "https://graph.facebook.com/pepsi")
============================================================
我使用过WebMock.disable_net_connect!(:allow => https://graph.facebook.com/ ),但问题仍然存在。
I had used WebMock.disable_net_connect!(:allow => "https://graph.facebook.com/") but problem still continue.
模型文件:
MODEL file:
def self.validate_fbid(fbid, format = :json)
uri = URI.parse("https://graph.facebook.com/#{fbid}")
data = Net::HTTP.get(uri)
return JSON.parse(data['username'])
end
SPEC文件:
before(:each) do
stub_request(:get, "https://graph.facebook.com/pepsi").to_return(:body => { 'username' => 'pepsi'})
end
describe "correct facebook id" do
#validate fbid
it "should validate fbid" do
Company.validate_fbid('pepsi')['username'].should == "pepsi"
WebMock.should have_requested(:get, "https://graph.facebook.com/pepsi")
end
end
控制器:
CONTROLLER :
def create
@company = Company.new(params[:company])
uri = URI("http://graph.facebook.com/" + @company.fbid)
data = Net::HTTP.get(uri)
username = JSON.parse(data)['username']
if !username.nil?
if @company.name.downcase == username.downcase
if @company.save
@message = "New company created"
redirect_to root_path
else
@message = "Company create attempt failed. Please try again."
render 'new'
end
else
@message = "Invalid facebook id"
render 'new'
end
else
@message = "No such facebook id"
render 'new'
end
end
推荐答案
您正在刷写HTTPS请求,但是使用端口443进行HTTP,这就是为什么Webmock没有找到正确的存根。
阅读将Net :: HTTP.get用于https网址
You are stubbing HTTPS request but doing an HTTP with port 443, That's why Webmock doesn't find the right stub. read Using Net::HTTP.get for an https url
这篇关于WebMock:Rspec - 使用JSON响应测试Facebook验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!