简体   繁体   中英

Functional Tests in Redmine?

I got some problems when writing the functional tests for my Redmine plugin. Well, I created a plugin in Redmine named Polls, in controller I defined two function named index and vote as bellow:

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

The index function will list all data in Polls table (the data get from sample data in polls.yml file and fetch to table Polls) and display inside each project was created. Here is my sample data in polls.yml file:

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

And the vote function will increment for each answer by one, the answer get from user. I have been defined the vote function in my model and invoked it at here. Block code of the vote function in Poll model:

class Poll < ActiveRecord::Base

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

For now, I writing two test cases in functional tests:

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 

I get an error while run test index:

# 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

and while run test vote:

# 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

Have a great day!

You have a suspicious code in PollsControllerTest :

assert_equal 'Vote saved.', expect(flash[:notice])

you probably mean:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM