Tag: rspec rails

使用RSpec测试奇异资源控制器

我在routes.rb定义了一个奇异的资源,看起来像这样 Rails.application.routes.draw do resource :dog, only: [:create], to: “dog#create”, controller: “dog” end 之后我用这样的创建动作定义了一个控制器 class DogController < ApplicationController def create render json: {}, status: :ok end end 而现在我正试图用这样的RSpec来测试它 require “rails_helper” describe DogController do it “works” do post :create, params: { foo: :bar } end end 这是抛出此错误而不是传递: ActionController::UrlGenerationError: No route matches {:action=>”create”, :controller=>”dog”, :foo=>:bar} 我究竟做错了什么?

Rspec – RuntimeError:nil的被调用id,错误地为4

我现在正在为ItemsController create操作进行Rspec测试,但是我遇到了错误。 (有Item , Auction , Bid , User等型号) create方法如下 # POST /items # POST /items.json def create @item = Item.new(params[:item]) @item.user_id = current_user.id respond_to do |format| if @item.save format.html { redirect_to @item, notice: ‘Item was successfully created.’ } format.json { render json: @item, status: :created, location: @item } else format.html { render action: “new” […]

在rspec中的控制器测试中使用rails“post”,并在路由上使用作用域和协议

我有一个rails项目,它正在生产中基本URL的子目录中运行,我希望它在dev中以这种方式运行,以使dev和prod尽可能接近。 我有路由文件设置如下: Foo::Application.routes.draw do def draw_routes root :to=>’foo#home’ resources :cats, :only=>[:create] do end if Rails.env.production? scope :protocol=>’http://’ do draw_routes end else scope :path=>”/foo”, :protocol=>’http://’ do draw_routes end end end 我的CatsController是这样的: class CatsController “Bob”) if @cat.save() redirect_to root end end end 我想测试我的Create Cat方法,所以我在spec / controllers / cats_controller_spec.rb中设置了一个rspec测试: require ‘spec_helper’ describe CatsController do describe “calling create cat […]

RSpec 1.3.3 Rails 2.3.9 Webrat 0.7.3“未定义方法`assign`”

我的规格 require ‘spec_helper’ describe ‘user_sessions/new.html.erb’ do let (:user_session) { mock_model(UserSession).as_null_object } before do assign(:user_session, user_session) end it ‘should have the sign in header’ do render rendered.should contain(‘Sign in’) end end 抛出 1) NoMethodError in ‘user_sessions/new.html.erb should have the sign in header’ undefined method `assign’ for # gem: group :test, :cucumber do gem “cucumber-rails”, “0.3.2” gem […]

在某些RSpec rails请求示例中测试HTTP状态代码,但是对于其他RSPro rails请求示例

在使用rspec-rails测试的Rails 4.2.0应用程序中,我提供了一个带有类似REST资源的JSON Web API,其强制属性为mand_attr 。 我想测试一下,当POST请求中缺少该属性时,此API会回答HTTP代码400( BAD REQUEST )。 (请参阅第二个示例。)我的控制器尝试通过抛出ActionController::ParameterMissing来生成此HTTP代码,如下面的第一个RSpec示例所示。 在其他 RSpec示例中,我希望通过示例(如果它们是预期的)挽救引发的exception或者命中测试运行器,因此它们会显示给开发人员(如果错误是意外的),因此我不想要去除 # Raise exceptions instead of rendering exception templates. config.action_dispatch.show_exceptions = false 来自config/environments/test.rb 我的计划是在请求规范中包含以下内容: describe ‘POST’ do let(:perform_request) { post ‘/my/api/my_ressource’, request_body, request_header } let(:request_header) { { ‘CONTENT_TYPE’ => ‘application/json’ } } context ‘without mandatory attribute’ do let(:request_body) do {}.to_json end it ‘raises a […]

使用Rspec进行测试时Zeus失败了

当我运行此命令时: rspec spec/controllers/messages_controller_spec.rb 我明白了: 完成0.54446秒 2个例子,0个失败 随机种子6226 很好。 但是,当我运行此命令时: zeus rspec spec/controllers/messages_controller_spec.rb 我明白了: 完成0.5356秒 2个例子,0个失败 随机种子0 这不是问题,但我马上也得到了这个: F。 失败: 1)MessagesController POST #create创建一个带有HTTP 200状态代码的消息 Failure/Error: post :create, message: “test message” NameError: undefined method `post’ for # # ./spec/controllers/messages_controller_spec.rb:10:in `block (3 levels) in ‘ # -e:1:in `’ 在0.10724秒完成2个示例,1个失败失败示例: rspec ./spec/controllers/messages_controller_spec.rb:9#MessagesController POST #create使用HTTP 200状态代码创建一条消息

Ruby / Rails / Rspec – ActiveRecord :: AssociationTypeMismatch:

好的,这是我的rspec代码…… before(:each) do @attr = { :greeting => “Lorem ipsum”, :recipient => @recipient } end it “should redirect to the home page” do puts “spec: @attr = #{@attr}” puts “spec: recipient = #{@attr[:recipient]}” post :create, :card => @attr response.should redirect_to(root_path) end 现在这个输出是: spec: @attr = {:greeting=>”Lorem ipsum”, :recipient=>#} spec: recipient = # 所以我们可以看到收件人是用户。 在控制器方面,我们看到…… def […]

RSpec不加载我的类,即使它在Rails控制台中加载

我在app/models/parser有一个名为data.rb ,内容如下: class Parser::Data def method1 end end 在这一点上没什么好看的。 我试图在实现太多之前为它编写测试,只是为Rails做了默认的RSpec安装。 我的RSpec文件在spec/models/parser/data_spec.rb ,到目前为止非常基础: require ‘spec_helper.rb’ describe Parser::Data do let(:parser) { Parser::Data.new } end 当我运行测试时,我收到此错误: spec/models/parser/data_spec.rb:3:in `’: uninitialized constant Parser (NameError) 我尝试将module Parser放在Data类的同一目录app/models/parser ,我也尝试将它移动到lib/parser做同样的模块包装类,并在application.rb添加了lib/parser到autoload但是到目前为止没有任何工作。 我究竟做错了什么?

使用RSpec的请求规范测试外部API

我正在尝试使用RSpec的请求规范将主机更改为指向远程URL而不是localhost:3000。 如果可能,请告诉我。 注意:只是想提一下远程URL只是一个JSON API。

RSpec:如何设置控制器的实例变量

我的rails控制器中有这个方法: def some_init_func # … @inst_var = 1 # … end 后来: do_something_with(@inst_var) 如何在RSpec中设置此实例变量? allow(controller).to receive(:some_init_func) do # ???? what goes here ???? end Muchas gracias 🙂