Redmine 中的功能测试?
Functional Tests in Redmine?
我在为我的 Redmine 插件编写功能测试时遇到了一些问题。
好吧,我在 Redmine 中创建了一个名为 Polls 的插件,在控制器中我定义了两个名为 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
index函数会列出Pollstable中的所有数据(数据取自polls.yml[=37中的示例数据=] 文件并获取到 table 民意调查)并在每个项目中显示已创建。
这是我在 polls.yml 文件中的示例数据:
poll_001:
id: 1
question: Can you see this poll?
is_yes: 2
is_no: 0
poll_002:
id: 2
question: And can you see this other poll?
is_yes: 1
is_no: 0
并且投票功能会为每个答案增加一个,答案来自用户。我已经在我的模型中定义了 vote 函数并在此处调用它。
Poll 模型中 vote 函数的代码块:
class Poll < ActiveRecord::Base
def vote(answer)
increment(answer == 'yes' ? :is_yes : :is_no)
end
end
目前,我在功能测试中编写了两个测试用例:
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
我在 运行 测试索引时出错:
# 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
同时 运行 测试投票:
# 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
祝你有美好的一天!
您在 PollsControllerTest
中有可疑代码:
assert_equal 'Vote saved.', expect(flash[:notice])
你的意思可能是:
assert_equal 'Vote saved.', flash[:notice]
我在为我的 Redmine 插件编写功能测试时遇到了一些问题。 好吧,我在 Redmine 中创建了一个名为 Polls 的插件,在控制器中我定义了两个名为 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
index函数会列出Pollstable中的所有数据(数据取自polls.yml[=37中的示例数据=] 文件并获取到 table 民意调查)并在每个项目中显示已创建。 这是我在 polls.yml 文件中的示例数据:
poll_001:
id: 1
question: Can you see this poll?
is_yes: 2
is_no: 0
poll_002:
id: 2
question: And can you see this other poll?
is_yes: 1
is_no: 0
并且投票功能会为每个答案增加一个,答案来自用户。我已经在我的模型中定义了 vote 函数并在此处调用它。 Poll 模型中 vote 函数的代码块:
class Poll < ActiveRecord::Base
def vote(answer)
increment(answer == 'yes' ? :is_yes : :is_no)
end
end
目前,我在功能测试中编写了两个测试用例:
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
我在 运行 测试索引时出错:
# 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
同时 运行 测试投票:
# 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
祝你有美好的一天!
您在 PollsControllerTest
中有可疑代码:
assert_equal 'Vote saved.', expect(flash[:notice])
你的意思可能是:
assert_equal 'Vote saved.', flash[:notice]