0

誰かが、Shoulda、Factory Girl、および Mocha を使用して Captcha 検証 + Authlogic をテストするための戦略/コード サンプル/ポインターを提供できますか?

たとえば、私の UsersController は次のようなものです。

class UsersController < ApplicationController
validates_captcha

...
def create
...
if captcha_validated?
      # code to deal with user attributes
end
...
end

この場合、Shoulda / Factory Girl / Mocha を使用してどのようにモック/スタブを作成し、Captcha 画像に対する有効な応答と無効な応答をテストしますか?

あなたの助けに感謝します、シヴァ

4

2 に答える 2

0

この設定で解決できました:

class UsersControllerTest < ActionController::TestCase

  context "create action" do

    context "valid user with valid captcha" do

      setup do
        User.any_instance.stubs(:valid?).returns(true)
        @controller.stubs(:captcha_validated?).returns(true)

        post :create, :user => Factory.attributes_for(:user, :captcha => "blahblah")
      end

      should_redirect_to("user home") { user_path(@user) }
    end

    context "valid user with invalid captcha" do
      setup do

        User.any_instance.stubs(:valid?).returns(true)
        @controller.stubs(:captcha_validated?).returns(false)

        post :create, :user => Factory.attributes_for(:user, :captcha => "blahblah")
      end

      should_render_template :new

    end
  end
end

ありがとう。

于 2010-06-07T21:36:51.763 に答える
0

がどこで定義されているかによると思いますcaptcha_validated?が、その戻り値をモックしてから、ケースごとにテストを書きたいと思います。このようなもの:

describe UsersController, "POST create" do
  context "valid captcha" do
    before do
      SomeCaptchaObject.expects(:captcha_validated?).returns(true)
    end
    # ...
  end
  context "invalid captcha" do
    before do
      SomeCaptchaObject.expects(:captcha_validated?).returns(false)
    end
    # ...
  end
end
于 2010-06-06T22:01:55.350 に答える