Tag: rspec

如何在整个示例组中使用实例变量,即使它在示例之外?

我正在使用Ruby on Rails 3.2.2和rspec-rails-2.8.1。 我想在整个Example Group中使用一个实例变量(在before hook中初始化),即使它在Example之外。 也就是说,我想做以下事项: describe “…” do before(:each) do @user = User.create(…) end # Here I would like to use the instance variable but I get the error: # “undefined method `firstname’ for nil:NilClass (NoMethodError)” @user.firstname it “…” do # Here it works. @user.firstname … end end 可能吗? 如果是这样,怎么样? 注意 :我想这样做是因为我试图输出有关将要运行的测试的更多信息,这样: […]

配置Warden以用于RSpec控制器规范

我能够使用Devise的sign_in方法在我的控制器规范中登录用户。 但是现在我正在从我的应用程序中删除Devise,我不太确定如何使用Warden自己的类似function。 我应该如何设置spec/spec_helper.rb和相关的spec/support/*.rb spec/spec_helper.rb文件,以使Warden在控制器规格内运行得足够多? 我尝试使用以下内容在spec/support/warden.rb设置文件: RSpec.configure do |config| config.include Warden::Test::Helpers config.after do Warden.test_reset! end end 然后我before调用类似于此beforevalidationuser工厂: before { login_as FactoryGirl.create(:user) } 但这是我一直看到的错误: NameError: undefined method `user’ for nil:NilClass 此错误追溯到我的authenticate_user! 控制器中的方法: def authenticate_user! redirect_to login_path, notice: “You need to sign in or sign up before continuing.” if env[‘warden’].user.nil? end 我很感激任何人都可以提供的任何指导。

使用rspec codechool 5级测试进行测试4

以下是测试的基本问题: We’ve changed the code below so we’re mocking ZombieMailer.tweet method instead of the entire method. There is still more to do to make this example work. First, finish the let(:mail) statement below by creating a stub with a deliver method that returns true. Then update the mock so the call to the tweet method […]

如何使用rspec检查变量是否是模块子类的实例?

我有一个类似于以下的类结构: module MyModule class MyOuterClass class MyInnerClass end end end 我正在尝试确保使用Rspec将变量正确实例化为MyInnerClass。 打印类的类型,它是MyModule :: MyOuterClass :: MyInnerClass。 但是,如果我尝试运行该行 expect{@instance_of_MyInnerClass}.to be_an_instance_of(MyModule::MyOuterClass::MyInnerClass) 我收到错误“你必须通过一个参数而不是一个块来使用提供的匹配器。” 此外,课程在另一个地方,所以我不能只检查 […] be_an_instance_of(MyInnerClass) Rspec抱怨MyInnerClass是一个未初始化的常量。 所以,我想问一下如何使用RSpecvalidation变量是否是MyInnerClass的实例。

Rails response.should be_success永远不会成真

我正在关注Michael Hartl关于Ruby on Rails的优秀教程。 我试图理解ActionDispatch :: Response的工作方式。 这源于第9章的练习9(Rails版本3.2.3)。 特别要求我们确保管理员用户无法User#destroy自己。 我知道如何做到这一点,但由于我正在尝试遵循TDD方法,我首先编写测试。 这是我测试中的相关片段: describe “authorization” do describe “as non-admin user” do let(:admin) {FactoryGirl.create(:admin)} let(:non_admin) {FactoryGirl.create(:user)} before{valid_signin non_admin} describe “submitting a DELETE request to the Users#destroy action” do before do delete user_path(admin) #puts response.message puts response.succes? end specify{ response.should redirect_to(root_path) } specify{ response.should_not be_success } end end #Exercise […]

Rails rspec undefined方法`receive_message’用于#<RSpec :: ExampleGroups ::

我试图在before(:each)内部模拟一些数据并得到 NoMethodError: undefined method `receive_message’ for # 我before(:each before(:each) do @rates = {“exchangerates”=> {“row”=> [{“exchangerate”=>{“ccy”=>”EUR”, “base_ccy”=>”UAH”, “buy”=>”28.33917”, “sale”=>”28.33917”}}, {“exchangerate”=>{“ccy”=>”RUR”, “base_ccy”=>”UAH”, “buy”=>”0.38685”, “sale”=>”0.38685”}}, {“exchangerate”=>{“ccy”=>”USD”, “base_ccy”=>”UAH”, “buy”=>”24.88293”, “sale”=>”24.88293”}}, {“exchangerate”=>{“ccy”=>”BTC”, “base_ccy”=>”USD”, “buy”=>”605.3695”, “sale”=>”669.0926”}}]}} obj = double() @request = allow(obj).to receive_message(@rates) end 怎么解决?

Rspec POST导致未定义的方法`key?’ for:create:Symbol

在测试我的控制器时,我在Rspec中使用factorygirl获取数据; describe ‘POST #create’ do context ‘with valid attributes’ do it ‘saves the new child in the database’ do expect{ post ‘/children’, :create, person: attributes_for(:child) }.to change(Person, :count).by(1) end 我收到一个错误 1)PeopleController POST #create具有有效属性将新子节点保存在数据库中失败/错误:发布’/ children’,:create,person:attributes_for(:child)NoMethodError:undefined method key?’ for :create:Symbol # ./spec/controllers/people_controller_spec.rb:46:in key?’ for :create:Symbol # ./spec/controllers/people_controller_spec.rb:46:in在’./spec/controllers/people_controller_spec.rb:45:in`block(4 levels)in’中的块(5级) 我的控制器(这就是对不相关的东西感到遗憾的一切) class PeopleController < ApplicationController before_action :set_person, only: [:show, […]

Rspec表示,为什么机架式磁盘不能过滤传入的请求

我希望我的Rails 5 API专用应用程序,现在运行在http://localhost:3000 ,只接受来自我的NodeJS前端应用程序的请求,现在运行在http://localhost:8888 。 所以我像这样配置了/config/initializers/cors.rb : Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins “http://localhost:8888” resource “*”, headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head] end end 我写了这个测试: #/spec/request/cors_request_spec.rb RSpec.feature “CORS protection”, type: :request do it “should accept a request from a whitelisted domain” do get “/api/v1/bodies.json”, nil, “HTTP_ORIGIN”: “http://localhost:8888” expect(response.status).to eql(200) end […]

*** RuntimeErrorexception:ActionView :: Helpers :: ControllerHelper#response委托给controller.response,但是控制器是nil

当我尝试运行端点的api规范时,我遇到了一个问题。 当我运行规范并检查响应时,它说: *** RuntimeError Exception: ActionView::Helpers::ControllerHelper#response delegated to controller.response, but controller is nil: #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007faeb4852b08 我在controllers / api / v1 / catalog_controller.rb下有一个控制器 class Api::V1::CatalogController < ApplicationController def search …some stuff here… render json: {some_response} end end 它的匹配规范在spec / controllers / api / v1 / catalog_controller_spec.rb下 describe Api::V1::CatalogController do describe “searching” do before(:all) do @title = FactoryGirl.create(:title, […]

Rails – 如何为存根/伪造的http或ajax响应存根特定的响应时间

我想测试一个function,当我的应用程序在Rails UJS / ajax超时时发送usera自定义消息。 超时Rails UJS请求的代码本身就在应用程序上: $.rails.ajax = function(options) { if (!options.timeout) { options.timeout = 10000; } return $.ajax(options); }; 当观察chrome dev工具在我的本地开发模式下超时时发生的事情时,我注意到代码状态奇怪地是200但是当它“超时”时,我的消息确实显示给用户。 on(‘ajax:error’,function(event,xhr, status, error){ // display message in modal for users if(status == “timeout”) { console.log( ‘ ajax request timed out’); var msg; msg = Messenger().post({ hideAfter: 8, message: “Too long, the app timed […]