Tag: function测试

如何将Rails助手导入function测试

嗨,我最近inheritance了一个项目,其中前开发人员不熟悉rails,并决定在视图助手中加入许多重要的逻辑。 class ApplicationController < ActionController::Base protect_from_forgery include SessionsHelper include BannersHelper include UsersHelper include EventsHelper end 特别是会话管理。 这是可以的,并与应用程序一起工作,但我在为此编写测试时遇到问题。 一个具体的例子。 某些操作执行before_filter以查看current_user是否为admin。 这个current_user通常由我们所有控制器共享的sessions_helper方法设置所以为了正确测试我们的控制器,我需要能够使用这个current_user方法 我试过这个: require ‘test_helper’ require File.expand_path(‘../../../app/helpers/sessions_helper.rb’, __FILE__) class AppsControllerTest @app.attributes end end require语句发现session_helper.rb没问题,但没有Rails魔法,它在AppsControllerTest无法以相同的方式AppsControllerTest 我怎么能欺骗这个疯狂的设置进行测试?

使用Rails和Devise进行function测试。 把什么放在我的灯具里?

嗨,我想对使用Devise和CanCan的Rails 3应用程序进行一些function测试。 在我的用户模型中,我有用户年龄,我想测试一个用户只能访问某个页面,如果它们是: 登录 超过35岁 我在Devise文档中看到我可以使用:* sign_in *并且我把它放在我的测试中它似乎工作 – 测试没有错误,因为我有: include Devise::TestHelpers 在我的* test_helper.rb * 当我拿出它时,我的测试会出错,因为* sign_in *没有定义。 所以这不是帮手问题。 当我运行测试并检查span#loggedin是否有一次出现时,测试失败,因为有0次出现。 span#loggedin仅出现* if user_signed_in?* 我需要在我的灯具或测试中创建一个也是完全注册用户(已确认等)的测试用户? 查看代码: User is signed in User age is 测试代码: test “should get index” do sign_in :one get :index assert_response :success assert_select ‘span#loggedin’, :count => 1 end 夹具: one: email: jez@example.com age: […]

auth-hmacfunction测试问题

我想在我的api应用程序中使用这个gem https://github.com/seangeo/auth-hmac/ 我有一个关于为请求validation创建测试的问题。 我想用hmac签署请求,但rails controller在下一个代码后没有http头 def setup #load from fixture @client = clients(:client_2) end def sign_valid_request(request,client) auth_hmac = AuthHMAC.new(client.key => client.secret ) auth_hmac.sign!(request,client.key) request end def test_response_client_xml @request = sign_valid_request(@request,@client) get :index , :api_client_key => @client.key , :format=> “xml” @xml_response = @response.body assert_response :success assert_select ‘id’ , @client.id.to_s end 路由有这样的配置 scope ‘/:token/’ do # route […]

Mocha Mock进行另一项测试

我一直在遵循15个TDD步骤来创建Rails应用程序指南 – 但是遇到了一个我似乎无法解决的问题。 对于WordsController的function测试,我有以下代码: class WordsControllerTest < ActionController::TestCase test "should get learn" do get 'learn' assert_response :success end test "learn passes a random word" do some_word = Word.new Word.expects(:random).returns(some_word) get 'learn' assert_equal some_word, assigns('word') end end 在Word类中,我有以下代码: class Word < ActiveRecord::Base def self.random all = Word.find :all all[rand(all.size)] end end 当我运行测试时,我遇到以下错误(为简洁起见缩短): 1) Failure: unexpected invocation: […]