在为我的红矿插件编写功能测试时,我遇到了一些问题。嗯,我在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
索引函数将列出轮询表中的所有数据(从polls.yml文件中的样本数据中获取数据并将其提取到表轮询中),并在每个项目中显示。下面是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
而投票函数对每一个答案都会增加一个,答案从用户那里得到。我在我的模型中定义了投票函数,并在这里调用了它。选票模型中投票函数的分块代码:
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
祝你有愉快的一天!
发布于 2015-09-09 07:42:24
您在PollsControllerTest
中有一个可疑代码
assert_equal 'Vote saved.', expect(flash[:notice])
你可能是说:
assert_equal 'Vote saved.', flash[:notice]
https://stackoverflow.com/questions/32473516
复制相似问题