免责声明:我是 API 新手,在尝试解决这个看似简单的问题一周后,我已经迷失了方向。
我有与已正确实现的 Github API 交互的代码,但我想返回并使用 RSpec、Webmock 和 VCR 对其进行测试。
基本上,我有一个#create
方法可以让用户在 Github 上创建一个新的存储库。
这是该方法的代码:
def self.create(属性)
post = HTTParty.post(
'https://api.github.com/user/repos',
:headers => {
'User-Agent' => 'Githubrepo',
'Content-Type' => 'application/json',
'Accept' => 'application/json'
},
:basic_auth => {
:username => attributes[:username],
:password => attributes[:password]
},
:body => {
'name' => attributes[:repository],
'description' => attributes[:description]
}.to_json
)
Githubrepo.parse_response_from(post, attributes[:wants_ssh])
end
这一切所做的就是POST
使用给定的某些属性发出实际请求,并将发布的信息存储到一个名为的变量post
中,然后将其作为属性传递给另一个方法#parse_response_from
。这种#parse_response_from
方法是我想测试的,但我一生都无法理解如何post
用 Webmock 和 VCR 模拟属性。
这就是我需要模仿的 post 变量在生产中的样子:
{"id"=>23100237,
"name"=>"test_repository",
"full_name"=>"willia15/test_repository",
"owner"=>
{"login"=>"willia15",
"id"=>4252986,
"avatar_url"=>"https://avatars.githubusercontent.com/u/4252986?v=2",
"gravatar_id"=>"a64a6453a1d8e9119da938630a8cca33",
"url"=>"https://api.github.com/users/willia15",
"html_url"=>"https://github.com/willia15",
"followers_url"=>"https://api.github.com/users/willia15/followers",
"following_url"=>
"https://api.github.com/users/willia15/following{/other_user}",
"gists_url"=>"https://api.github.com/users/willia15/gists{/gist_id}",
"starred_url"=>
"https://api.github.com/users/willia15/starred{/owner}{/repo}",
"subscriptions_url"=>"https://api.github.com/users/willia15/subscriptions",
"organizations_url"=>"https://api.github.com/users/willia15/orgs",
"repos_url"=>"https://api.github.com/users/willia15/repos",
"events_url"=>"https://api.github.com/users/willia15/events{/privacy}",
"received_events_url"=>
"https://api.github.com/users/willia15/received_events",
"type"=>"User",
"site_admin"=>false},
"private"=>false,
"html_url"=>"https://github.com/willia15/test_repository",
"description"=>nil,
"fork"=>false,
"url"=>"https://api.github.com/repos/willia15/test_repository",
"forks_url"=>"https://api.github.com/repos/willia15/test_repository/forks",
"keys_url"=>"https://api.github.com/repos/willia15/test_repository/keys{/key_id}",
"collaborators_url"=>
"https://api.github.com/repos/willia15/test_repository/collaborators{/collaborator}",
"teams_url"=>"https://api.github.com/repos/willia15/test_repository/teams",
"hooks_url"=>"https://api.github.com/repos/willia15/test_repository/hooks",
"issue_events_url"=>
"https://api.github.com/repos/willia15/test_repository/issues/events{/number}",
"events_url"=>"https://api.github.com/repos/willia15/test_repository/events",
"assignees_url"=>
"https://api.github.com/repos/willia15/test_repository/assignees{/user}",
"branches_url"=>
"https://api.github.com/repos/willia15/test_repository/branches{/branch}",
"tags_url"=>"https://api.github.com/repos/willia15/test_repository/tags",
"blobs_url"=>"https://api.github.com/repos/willia15/test_repository/git/blobs{/sha}",
"git_tags_url"=>
"https://api.github.com/repos/willia15/test_repository/git/tags{/sha}",
"git_refs_url"=>
"https://api.github.com/repos/willia15/test_repository/git/refs{/sha}",
"trees_url"=>"https://api.github.com/repos/willia15/test_repository/git/trees{/sha}",
"statuses_url"=>
"https://api.github.com/repos/willia15/test_repository/statuses/{sha}",
"languages_url"=>"https://api.github.com/repos/willia15/test_repository/languages",
"stargazers_url"=>"https://api.github.com/repos/willia15/test_repository/stargazers",
"contributors_url"=>
"https://api.github.com/repos/willia15/test_repository/contributors",
"subscribers_url"=>
"https://api.github.com/repos/willia15/test_repository/subscribers",
"subscription_url"=>
"https://api.github.com/repos/willia15/test_repository/subscription",
"commits_url"=>"https://api.github.com/repos/willia15/test_repository/commits{/sha}",
"git_commits_url"=>
"https://api.github.com/repos/willia15/test_repository/git/commits{/sha}",
"comments_url"=>
"https://api.github.com/repos/willia15/test_repository/comments{/number}",
"issue_comment_url"=>
"https://api.github.com/repos/willia15/test_repository/issues/comments/{number}",
"contents_url"=>
"https://api.github.com/repos/willia15/test_repository/contents/{+path}",
"compare_url"=>
"https://api.github.com/repos/willia15/test_repository/compare/{base}...{head}",
"merges_url"=>"https://api.github.com/repos/willia15/test_repository/merges",
"archive_url"=>
"https://api.github.com/repos/willia15/test_repository/{archive_format}{/ref}",
"downloads_url"=>"https://api.github.com/repos/willia15/test_repository/downloads",
"issues_url"=>
"https://api.github.com/repos/willia15/test_repository/issues{/number}",
"pulls_url"=>"https://api.github.com/repos/willia15/test_repository/pulls{/number}",
"milestones_url"=>
"https://api.github.com/repos/willia15/test_repository/milestones{/number}",
"notifications_url"=>
"https://api.github.com/repos/willia15/test_repository/notifications{?since,all,participating}",
"labels_url"=>"https://api.github.com/repos/willia15/test_repository/labels{/name}",
"releases_url"=>
"https://api.github.com/repos/willia15/test_repository/releases{/id}",
"created_at"=>"2014-08-19T06:59:50Z",
"updated_at"=>"2014-08-19T06:59:50Z",
"pushed_at"=>"2014-08-19T06:59:50Z",
"git_url"=>"git://github.com/willia15/test_repository.git",
"ssh_url"=>"git@github.com:willia15/test_repository.git",
"clone_url"=>"https://github.com/willia15/test_repository.git",
"svn_url"=>"https://github.com/willia15/test_repository",
"homepage"=>nil,
"size"=>0,
"stargazers_count"=>0,
"watchers_count"=>0,
"language"=>nil,
"has_issues"=>true,
"has_downloads"=>true,
"has_wiki"=>true,
"forks_count"=>0,
"mirror_url"=>nil,
"open_issues_count"=>0,
"forks"=>0,
"open_issues"=>0,
"watchers"=>0,
"default_branch"=>"master",
"permissions"=>{"admin"=>true, "push"=>true, "pull"=>true},
"network_count"=>0,
"subscribers_count"=>1}
基本上,我真正需要从这个散列中传递一些变量到#parse_response_from
方法中。我现在测试的方式只是将某些静态键/值对存储到 before 块中并测试该代码。看一下:
before(:each) do
@api_data = { "name" => "test_repo",
"ssh_url" => "git@github.com:test_user/test_repo.git",
"clone_url" => "https://github.com/test_user/test_repo.git"
}
end
如何使用 Webmock 将其存根stub_request
并将其传递到 VCR 磁带?我已经看到了很多存根的示例GET
,但我不明白如何使用 Webmock 为POST
. 具体来说,我需要了解如何从 Webmock 存根返回post
变量中显示的数据。
我正在寻找一个特定的示例或半示例来指导我朝着正确的方向前进,而不是建议在其他地方查看某些文档,因为我将再次绕圈运行。