ruby-on-rails - POST 成员路由的 Rspec 测试失败

标签 ruby-on-rails ruby-on-rails-3 testing rspec factory-bot

我有一个多态投票模型,它通过其父模型上的成员路由进行 POST。每个 parent 在 Controller 中都有一个“投票”方法,我试图在我的 Controller 测试中测试这个 Action 。请查看下面的代码,为简单起见,我将展示 Controller 操作和测试之一。

不管我的努力和尝试 id: @answer, id: @vote, FactoryGirl.attributes_for(:vote, user_id: @user2) 进入 expect proc,我无法让这个测试通过。

工厂女孩.rb

FactoryGirl.define do
  factory :user do |u|
    u.sequence(:email) {|n| "test#{n}@hotmail.com"}
    u.sequence(:username) {|n| "tester#{n}" }
    u.password "password"
    u.password_confirmation "password" 
    u.remember_me true
    u.reputation 200
  end

factory :answer do
    user_id :user
    question_id :question
    body "you need to change your grip"
    votes_count 0
    correct true
  end

factory :vote do
    user_id :user
    votable_id :answer
    votable_type "Answer"
    value 1
  end

Answers_controller_spec.rb

describe "POST vote" do
    it "creates vote" do
      @user2 = FactoryGirl.create(:user)
      @answer = FactoryGirl.create(:answer)
      @vote = FactoryGirl.create(:vote)
      expect {
        post :vote, id: @vote 
      }.to change(Vote, :count).by(1)
      response.should be_success
    end
  end

测试失败

Failures:

  1) AnswersController POST vote creates vote
     Failure/Error: expect {
       count should have been changed by 1, but was changed by 0
     # ./spec/controllers/answers_controller_spec.rb:90:in `block (3 levels) in <top (required)>'

Finished in 2.01 seconds
9 examples, 1 failure

投票.rb

class Vote < ActiveRecord::Base

  attr_accessible :value, :votable_id, :votable_type
  belongs_to :votable, polymorphic: true
  belongs_to :user

  validates_inclusion_of :value, in: [1, -1]
  validates_presence_of :user_id, :value, :votable_id, :votable_type, :points
  validates_uniqueness_of :user_id, scope: :votable_id
  validates_uniqueness_of :value, scope: :votable_id

  before_save :create_points

  def create_points
    self.value == 1 ? self.points = 5 : self.points = -3
  end
end

路线.rb

  resources :answers do 
    member { post :vote }
    member { put :correct }
    resources :comments, except: [:edit, :update]
  end

answers_controller.rb

 def vote 
  @vote = current_user.votes.build(value: params[:value], votable_id: params[:id], votable_type: "Answer")
    respond_to do |format|
    if @vote.save
      format.html { redirect_to :back, notice: "Vote submitted" }
      format.js
    else
      format.html { redirect_to :back, alert: "You can't vote on your own content" }
      format.js
    end
  end
end

最佳答案

id参数是答案的id:

post :vote, id: @answer.id

您可能还需要传递 value 参数(可能是“向上”或“向下”值?)。

测试失败可能还有其他原因,例如Vote 模型中的验证。如果 vote.save 返回 false,则检查 errors 集合。

以下评论:

您需要将 作为参数传递给规范中的post。看起来你也有重复的投票,你可能根本不需要在你的规范中创建 @voteerrors 集合由 valid? 方法填充,该方法由 save 调用。

关于ruby-on-rails - POST 成员路由的 Rspec 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17187436/

相关文章:

ruby-on-rails - 在 Controller 中渲染 View 后出现 nil 的 NoMethodError

ruby-on-rails-3 - 或查询轮胎/弹性

node.js - 如何使用 local.example.js ENV 运行测试

ruby-on-rails - 安装发动机的 RSpec 测试路线

css - 无法将 Bower 组件 css 添加到 Rails 4.2 中的 Assets 管道中

ruby-on-rails - Rails 4:嵌套属性和 PG::NotNullViolation 错误

ruby-on-rails-3 - 使用 STI 时使用 Thinking Sphinx 范围获取 "undefined method"

ruby - 针对外部 UDP API 进行测试。从哪儿开始?

node.js - Express API 集成测试 : Error: timeout of 2000ms exceeded. 确保在此测试中调用 done() 回调

mysql - rails 3;事件记录;在哪里;数据库中两列之间的 NOT EQUAL 条件比较