首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >红矿山的功能测试?

红矿山的功能测试?
EN

Stack Overflow用户
提问于 2015-09-09 07:34:12
回答 1查看 103关注 0票数 0

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

代码语言:javascript
运行
复制
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文件中的示例数据:

代码语言:javascript
运行
复制
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

而投票函数对每一个答案都会增加一个,答案从用户那里得到。我在我的模型中定义了投票函数,并在这里调用了它。选票模型中投票函数的分块代码:

代码语言:javascript
运行
复制
class Poll < ActiveRecord::Base

    def vote(answer)
        increment(answer == 'yes' ? :is_yes : :is_no)
    end
end

目前,我在功能测试中编写了两个测试用例:

代码语言:javascript
运行
复制
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 

在运行测试索引时,我得到了一个错误:

代码语言:javascript
运行
复制
# 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

在运行测试投票时:

代码语言:javascript
运行
复制
# 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

祝你有愉快的一天!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-09 07:42:24

您在PollsControllerTest中有一个可疑代码

代码语言:javascript
运行
复制
assert_equal 'Vote saved.', expect(flash[:notice])

你可能是说:

代码语言:javascript
运行
复制
assert_equal 'Vote saved.', flash[:notice]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32473516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档