我是 Ruby 和 RSpec 的新手。我来自 Java 背景,这就是为什么我的测试看起来真的像 junit 代码。我正在尝试了解有关 RSpec 的更多信息,但我不太了解subject
, let
, !let
. 所以,如果有人能指导我清理这段代码,我将不胜感激。
我有 sinatra,RSpec,它正在使用 Twitter 登录。
get '/login/twitter' do
begin
request_token = TwitterService.new.authentication_request_token
session[:request_token_twitter] = request_token
redirect request_token.authorize_url
rescue Exception => e
logger.error(e.message)
redirect '/'
end
end
get '/login/twitter/success' do
request_token = session[:request_token_twitter]
twitter_service = TwitterService.new
access_token = twitter_service.authorize(request_token, params[:oauth_verifier])
begin
twitter_user_info = twitter_service.verify_credentials
twitter_id = twitter_user_info["id"]
response.set_cookie("auth_token", :value => twitter_id, :path => '/')
response.set_cookie(@social_flag, :value => "t", :path => '/')
expected_user = @user_manager.find_by_id(twitter_id.to_s)
if expected_user.is_null?
twitter_user = User.new(twitter_id, access_token.token, access_token.secret, "t")
twitter_user.save
logger.info("Saving ...")
logger.info("Twitter ID #{twitter_id}")
redirect '/signup'
else
expected_user.token = access_token.token
expected_user.secret = access_token.secret
expected_user.update
logger.info("Updating token and secret ...")
logger.info("Twitter ID #{twitter_id}")
end
rescue Exception => e
logger.error(e.message)
logger.error("There's something wrong with Twitter and user cannot log in")
redirect '/'
end
redirect '/t'
end
这是我的 RSpec。我知道这真的很丑。
describe "Twitter route" do
include TwitterOAuth
def app
Sinatra::Application
end
context "/login/twitter" do
it "should redirect to twitter authorized url" do
request_token = OpenStruct.new
request_token.authorize_url = "http://api.twitter.com/oauth/authenticate?oauth_token"
TwitterService.any_instance.stub(:authentication_request_token).and_return(request_token)
get '/login/twitter'
last_response.header["Location"].should include "http://api.twitter.com/oauth/authenticate?oauth_token"
last_response.status.should eql 302
session[:request_token_twitter].authorize_url.should == "http://api.twitter.com/oauth/authenticate?oauth_token"
end
it "should redirect back to home page if error occurs" do
TwitterService.any_instance.stub(:authentication_request_token).and_raise("Unauthorized")
get '/login/twitter'
last_response.header["Location"].should include "http://example.org/"
last_response.status.should eql 302
session[:request_token_twitter].should eql nil
end
it "should save a user after a success callback from twitter" do
user_manager = UserManager.new
access_token = OpenStruct.new
access_token.token = "token"
access_token.secret = "secret"
TwitterService.any_instance.stub(:authorize).with(anything(), anything()).and_return(access_token)
TwitterService.any_instance.stub(:verify_credentials).and_return({"id" => "id1"})
get '/login/twitter/success'
last_response.header["Location"].should include "/signup"
rack_mock_session.cookie_jar["auth_token"].should eql "id1"
rack_mock_session.cookie_jar["s_flag"].should eql "t"
last_response.status.should eql 302
user_manager = UserManager.new
expected_user = user_manager.find_by_id("id1")
expected_user.id.should eql "id1"
expected_user.token.should eql "token"
expected_user.secret.should eql "secret"
end
it "should update user token and secret if the user already exists" do
User.new("id1", "token", "secret", "t").save
access_token = OpenStruct.new
access_token.token = "token1"
access_token.secret = "secret1"
TwitterService.any_instance.stub(:authorize).with(anything(), anything()).and_return(access_token)
TwitterService.any_instance.stub(:verify_credentials).and_return({"id" => "id1"})
get '/login/twitter/success'
last_response.header["Location"].should include "/t"
rack_mock_session.cookie_jar["auth_token"].should eql "id1"
rack_mock_session.cookie_jar["s_flag"].should eql "t"
last_response.status.should eql 302
user_manager = UserManager.new
expected_user = user_manager.find_by_id("id1")
expected_user.id.should eql "id1"
expected_user.token.should eql "token1"
expected_user.secret.should eql "secret1"
end
it "should redirect back to the home page" do
access_token = OpenStruct.new
access_token.token = "token1"
access_token.secret = "secret1"
TwitterService.any_instance.stub(:authorize).with(anything(), anything()).and_return(access_token)
TwitterService.any_instance.stub(:verify_credentials).and_raise
get '/login/twitter/success'
last_response.header["Location"].should include "http://example.org/"
end
end
end
任何改进我都将不胜感激,而不仅仅是代码。可能是我错过了一些明显的东西。
非常感谢你们。