1

私のコントローラー仕様の1つで本当に奇妙なrspecの動作を取得しています。

説明するのが最善です。ルビミンでは、ブレークポイントを設定すると、次のようになります。

#rspec test
describe Api::V1::UsersController do
  let(:user) { FactoryGirl.create(:user) }
  describe "#show" do
    it "responds successfully" do
      get 'show', id: user.id
      response.should be_success
    end
end

#controller
class Api::V1::UsersController < AuthenticatedController
    def show # !!! RubyMine breakpoint will stop execution here !!!
      user = User.find(params[:id])
      user_hash = User.information(user, current_user)

      respond_to do |format|
        format.json { render json: user_hash.to_json }
      end
end

したがって、上記は期待どおりに機能します。

しかし、今ではこのテストは失敗します。

#rspec test
describe UsersController do
  let(:user) { FactoryGirl.create(:user, is_admin: false) }
  describe "#show" do
    it "redirects non-admin" do
      get 'index'
      response.should redirect_to user_path(user)
    end
end

#controller
class UsersController < AuthenticatedController
  def index # !!! Breakpoint is never hit !!!
    @users = User.all
    respond_to do |format|
      if current_user.is_admin
        format.html
        format.json { render json: @users }
      else
        redirect_to user_path(current_user) and return
      end
    end
end
By the way, this is the result:
Expected response to be a redirect to <http://test.host/users/625> but was a redirect to <https://test.host/users>

UsersControllerのコントローラーメソッドのブレークポイントはどれもヒットしません。しかし、API :: V1 :: UsersControllerでブレークポイントを設定すると、すべてのコントローラーメソッドがヒットします。

どんなガイダンスでも大歓迎です。私はこれをデバッグする方法が本当に途方に暮れています。

4

2 に答える 2

2

申し訳ありませんが、この質問は何よりも欲求不満からのものでした。しかし、私はついに何が起こっているのかを理解しました。ヒント: tailtest.logを実行することをお勧めします。

コントローラにSSLを強制していました。送信されるリクエストrspecはhttpです。 ActionController::ForceSSLリクエストをhttpsと同じcontroller#actionにリダイレクトします。ただし、この時点で、rspecテストは終了し、同じcontroller#actionへのリダイレクトのみが表示されるため、テストに失敗しました。

したがって、before(:each)または同様の何かで、これを使用します: request.env['HTTPS'] = 'on'。現在、すべてのテストは期待どおりに機能します。

于 2013-02-03T23:43:01.607 に答える
1

リダイレクトテストに関して、ここでrspecのドメインの外に出ているのではないかと思います。カピバラとrspecの使用をお勧めしますか?

私の情報源: Rspec-Rails-リダイレクトに従う方法 http://robots.thoughtbot.com/post/33771089985/rspec-integration-tests-with-capybara

于 2013-02-03T01:09:30.550 に答える