1

我是 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

任何改进我都将不胜感激,而不仅仅是代码。可能是我错过了一些明显的东西。

非常感谢你们。

4

1 回答 1

7

好的,这里发生了很多事情!

首先,您应该尝试对每个示例进行一个测试。您的示例当前正在测试一大堆行为,这意味着您的测试是全有或全无,如果您破坏了某些东西,可能会不清楚具体破坏了什么。

首先,我要添加一个新的匹配器。你通常会把它放在像 spec/support/matchers.rb 之类的地方。它只是扩展 rspec 以便我们可以测试响应是重定向,并且重定向到给定位置:

RSpec::Matchers.define :redirect_to do |expected|
  match do |actual|
    actual.should be_redirect
    actual.location.should include expected
  end
end

现在,进入代码!

未注释的来源在这里:https ://gist.github.com/cheald/5908093 - 阅读起来可能不那么烦人:)

let定义了一个在每个示例中只运行一次的方法,无论它被调用多少次。这让我们有一个在示例时定义的“变量”,这让我们可以在嵌套示例中覆盖它。在这里,我已经access_token定义了顶部,但我们将let在更深入的示例中使用另一个 access_token。这个套件并不能很好地展示这一点,但这可以让你做一些好事,其中一个东西let被另一个引用。想象一下,如果你愿意,我们有

let(:user) { user_manager.find(access_token.id) }

这将使用嵌套最深的 user_manager 和最深嵌套的 access_token,而无需在每个嵌套范围内重新声明用户。便利!

let块在使用之前不会被调用(与let!块相反,块在声明时总是被调用)

describe "Twitter route" do
  include TwitterOAuth

  let(:app) {  Sinatra::Application }
  let(:request_token) { double("request_token", authorize_url: "http://api.twitter.com/oauth/authenticate?oauth_token") }
  let(:access_token) { double("token", token: "token", secret: "secret") }
  let(:user_manager) { UserManager.new }

您会注意到我已将您的测试分解为嵌套上下文,以对类似行为进行分组。也就是说,应该使用授权令牌通过的所有测试都嵌套在授权令牌上下文下,我们的before块设置上下文,以便此上下文中的所有示例都获得有效令牌。

我们还继续get在 before 块中执行,所以我们可以直接测试结果。

  context "/login/twitter" do
    context "with an authorized token" do
      before do
        TwitterService.any_instance.stub(:authentication_request_token).and_return(request_token)
        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'
      end

你在这里看到我正在使用我们的新匹配器。它让我们可以在一次测试中检查到给定 URL 的重定向。

      it "should redirect to twitter authorized url" do
        last_response.should redirect_to "http://api.twitter.com/oauth/authenticate?oauth_token"
      end

      it "should set a the request token in the session" do
        session[:request_token_twitter].authorize_url.should == "http://api.twitter.com/oauth/authenticate?oauth_token"
      end

      context "after a success callback" do
        let(:user) { user_manager.find_by_id("id1") }
        context "when there is not an existing user" do
          before do
            get '/login/twitter/success'
          end

          it "should redirect to /signup" do
            last_response.should redirect_to "/signup"
          end

          it "should set an auth_token cookie" do
            rack_mock_session.cookie_jar["auth_token"].should == "id1"
          end

          it "should set an s_flag cookie" do
            rack_mock_session.cookie_jar["s_flag"].should == "t"
          end

在这里你会看到subject。它只是定义变量subject返回的内容,并使its块对其进行操作。在这种情况下,主题是User记录。既然subject是用户记录,我可以用更简洁的形式来检查它的属性。

          context "the authenticated user" do
            subject { user }
            its(:id)     { should == "id1" }
            its(:token)  { should == "token" }
            its(:secret) { should == "secret" }
          end
        end

你会在这里看到我为access_token. 当这些示例运行时,顶部的 before 块方式(设置“授权令牌”)将使用此access_token方式,而不是那里定义的方式。这使我们可以使用特定于该特定上下文的变量覆盖用于设置上下文的变量。

        context "when there is an existing user" do
          let(:access_token) { double("token", token: "newtoken", secret: "newsecret") }
          before do
            User.new("id1", "oldtoken", "oldsecret", "t").save
            get '/login/twitter/success'
          end

          it "should set an auth_token cookie" do
            rack_mock_session.cookie_jar["auth_token"].should == "id1"
          end

          it "should set an s_flag cookie" do
            rack_mock_session.cookie_jar["s_flag"].should == "t"
          end

          it "should redirect to /t" do
            last_response.should redirect_to "/t"
          end

          context "the authenticated user" do
            subject { user }
            its(:id)     { should == "id1" }
            its(:token)  { should == "newtoken" }
            its(:secret) { should == "newsecret" }
          end
        end
      end
    end

    context "with an invalid token" do
      before do
        TwitterService.any_instance.stub(:authentication_request_token).and_raise("Unauthorized")
        get '/login/twitter'
      end

      it "should redirect back to home page if error occurs" do
        last_response.should redirect_to "http://example.org/"
      end

      it "should not set a session value" do
        session[:request_token_twitter].should be_nil
      end
    end
  end
end
于 2013-07-02T09:57:42.060 回答