我正在使用Rails进行RSpec测试,由于我分配给模型关联的命名空间模型,它失败了。
CLI:
Failures:
1) Blog::BlobsController GET index assigns all blog/blobs as @blobs
Failure/Error: expect(assigns(:blog::blobs)).to eq([blog::blobs])
NoMethodError:
undefined method `blobs' for :blog:Symbol
# ./spec/controllers/blog/blobs_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
规格/控制器/博客/ blobs_controller_spec.rb
RSpec.describe Blog::BlobsController, type: :controller do
describe "GET index" do
it "assigns all blog/blobs as @blobs" do
blobs = Blog::Blob.create!
get :index, {}
expect(assigns(:blog::blobs)).to eq([blog::blobs])
end
end
end
的routes.rb
# SNIPPED FOR BREVITY...
namespace :blog do
resources :blobs
end
应用/控制器/博客/ blobs_controller.rb
class Blog::BlobsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
def index
@blobs = Blog::Blob.all
end
def new
@blob = Blog::Blob.new
end
def edit
@blob = Blog::Blob.find(params[:id])
end
def create
@blob = Blog::Blob.new(blob_params)
if @blob.save
redirect_to @blob
else
render 'new'
end
end
def update
@blob = Blog::Blob.find(params[:id])
if @blob.update(blob_params)
redirect_to @blob
else
render 'edit'
end
end
def show
@blob = Blog::Blob.find(params[:id])
end
def destroy
@blob = Blog::Blob.find(params[:id])
@blob.destroy!
redirect_to blog_blobs_path
end
private
def blob_params
params.require(:blob).permit(:title, :body)
end
end
使用RSpec测试此控制器有更好的方法吗?
更新:
故障:
1) Blog::BlobsController GET #index assigns all widgets as @widgets
Failure/Error: expect(assigns(:blobs)).to eq([blob])
TypeError:
no implicit conversion of Symbol into Integer
# /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `[]'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `get'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/mongo-2.0.4/lib/mongo/client.rb:170:in `read_preference'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:318:in `default_read'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:251:in `read'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/mongo-2.0.4/lib/mongo/collection/view/iterable.rb:38:in `each'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/bundler/gems/mongoid-26f67146a7b7/lib/mongoid/query_cache.rb:207:in `each'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/bundler/gems/mongoid-26f67146a7b7/lib/mongoid/contextual/mongo.rb:116:in `each'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/bundler/gems/mongoid-26f67146a7b7/lib/mongoid/contextual.rb:20:in `each'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/bundler/gems/mongoid-26f67146a7b7/lib/mongoid/criteria.rb:48:in `entries'
# /usr/local/rvm/gems/ruby-2.1.5@rails4/bundler/gems/mongoid-26f67146a7b7/lib/mongoid/criteria.rb:48:in `=='
# ./spec/controllers/blog/blobs_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
更新了规范代码:
require 'rails_helper'
RSpec.describe Blog::BlobsController, type: :controller do
describe "GET #index" do
it "assigns all blobs as @blobs" do
blob = Blog::Blob.create!
get :index, {}
expect(assigns(:blobs)).to eq([blob])
end
end
end
答案 0 :(得分:1)
你的测试说:
blobs = Blog::Blob.create!
这很令人困惑,因为blobs
是复数,但你只创建了一个blob。首先将其重命名为blob
。然后expect(assigns(:blog::blobs)).to eq([blog::blobs])
应为expect(assigns(:blobs)).to eq([blob])
。
在索引操作中,您设置@blobs = Blog::Blob.all
。赋值对应于控制器的实例变量。没有命名空间。