Ruby on rails Redmine的功能测试?

Ruby on rails Redmine的功能测试?,ruby-on-rails,testing,redmine,Ruby On Rails,Testing,Redmine,我在为我的Redmine插件编写功能测试时遇到了一些问题。 我在Redmine中创建了一个名为Polls的插件,在controller中我定义了两个名为index和vote的函数,如下所示: class PollsController < ApplicationController unloadable before_filter :find_project, :authorize, :only => :index def index @polls = Poll.

我在为我的Redmine插件编写功能测试时遇到了一些问题。 我在Redmine中创建了一个名为Polls的插件,在controller中我定义了两个名为index和vote的函数,如下所示:

class PollsController < ApplicationController
  unloadable
  before_filter :find_project, :authorize, :only => :index
  def index
    @polls = Poll.all
  end

  def vote
    poll = Poll.find(params[:id])
    poll.vote(params[:answer])
    if poll.save
      flash[:notice] = 'Vote saved.'
    end
    redirect_to :action => 'index'
  end
  private

  def find_project
    # @project variable must be set before calling the authorize filter
    @project = Project.find(params[:project_id])
  end
end
投票函数将为每个答案增加1,答案来自用户。我已经在我的模型中定义了投票函数,并在这里调用了它。 投票模型中投票函数的块代码:

class Poll < ActiveRecord::Base

    def vote(answer)
        increment(answer == 'yes' ? :is_yes : :is_no)
    end
end
class Poll
现在,我在功能测试中编写了两个测试用例:

require File.expand_path('../../test_helper', __FILE__)
class PollsControllerTest < ActionController::TestCase
    fixtures :projects, :users, :roles, :members, :polls
    def setup
        @project = Project.find(1)
        User.current = User.find(2)
        @request.session[:user_id] = 2
        @poll = Poll.all
    end

    test "index" do
        Role.find(1).add_permission! :view_polls
        get :index, :project_id => @project.id
        assert_response :success
        assert_template 'index'
    end

    test "vote" do
        post :vote, {:id => 1, :answer => 'no'}
        assert_equal 'Vote saved.', expect(flash[:notice])
        assert_response :success
        assert_template 'index'
    end
end 
require File.expand_path('../../test_helper',uu文件)
类PollsControllerTest@project.id
断言:成功
断言模板“索引”
结束
测试“投票”吗
post:vote,{:id=>1,:answer=>no'}
断言等于“已保存投票”,期望(flash[:注意])
断言:成功
断言模板“索引”
结束
结束
我在运行测试索引时出错:

# Running:
F
Finished in 1.051095s, 0.9514 runs/s, 0.9514 assertions/s.
  1) Failure:
PollsControllerTest#test_index [polls_controller_test.rb:14]:
Expected response to be a <success>, but was <403>
1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
#运行:
F
以1.051095s、0.9514次运行/秒、0.9514次断言/秒的速度完成。
1) 失败:
PollsControllerTest测试索引[轮询控制器测试.rb:14]:
预期响应为a,但为
1次运行,1次断言,1次失败,0次错误,0次跳过
运行测试投票时:

# Running:

E

Finished in 0.288799s, 3.4626 runs/s, 0.0000 assertions/s.

  1) Error:
PollsControllerTest#test_vote:
NoMethodError: undefined method `expect' for #<PollsControllerTest:0x7c71c60>


1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
#运行:
E
以0.288799秒、3.4626次运行/秒、0.0000次断言/秒的速度完成。
1) 错误:
PollsControllerTest#测试投票:
NoMethodError:的未定义方法'expect'#
1次运行,0次断言,0次失败,1次错误,0次跳过

祝你今天愉快

您在
PollsControllerTest
中有可疑代码:

assert_equal 'Vote saved.', expect(flash[:notice])
你可能是说:

assert_equal 'Vote saved.', flash[:notice]

谢谢你,dimakura。它起作用了,但我在这个测试中遇到了另一个错误:预期的响应是a,但实际上应该是302。投票后,您将重定向到:action=>“index”
!我应该做些什么来消除此消息,以及如何获取is_yes或is_no属性的值。真诚地使用
assert\u response:redirect
而不是
assert\u response:success
非常感谢你,dimakura。以及如何在投票后获得属性的值!
assert_equal 'Vote saved.', flash[:notice]