Tag: rspec rails

Rspec:如何在私有方法中引发exception测试?

在私有方法中测试exception时出错。 如何在私有方法中引发exception,该方法是从公共方法调用的? 上市 def public_method private_method end 私人的 def private_method tries = 0 begin raise Product::StaleObjectError.new(“Product is changed while you were editing”) if stale_object? // Do some work raise Exception.new(“Total amount used is greater than approved”) if total_approved e if tries < MAX_RETRIES tries += 1 sleep(1 + tries) reload retry else raise Product::StaleObjectError("Product is […]

请求正文不同时的存根请求

嗨,我正在使用ruby-2.5.0和Rails 5处理RoR项目。我正在使用stub_request来存储一个http请求。 此http请求基本上是发送电子邮件的mailjet api请求。 所以,我不能只重复使用片段,rspec吐出来,因为身体每次执行都不同。 存根请求 stub_request(:post, “https://api.mailjet.com/v3/send”). with( body: “{\”FromEmail\”:\”abc@abc.ocm\”,\”FromName\”:\”abc\”,\”To\”:\”abc@abc.com\”,\”Subject\”:\”Forgot Password\”,\”Text-part\”:\”xV3SEtaoa1oyYYfYBt12pw\”}”, headers: { ‘Accept’=>’application/json’, ‘Accept-Encoding’=>’deflate’, ‘Authorization’=>’Basic YmY0NzdhZmYyMmZlMGQwMzAxNzYzNDNkODUwZTY1MzE6YmRhNmZmYzQ0Y2ZmNGM3MmZkZGNkMjUwODA5YWJhZjM=’, ‘Content-Length’=>’143’, ‘Content-Type’=>’application/json’, ‘Host’=>’api.mailjet.com’, ‘User-Agent’=>’mailjet-api-v3-ruby/1.5.4’ }). to_return(status: 200, body: “”, headers: {}) 这里Text-part对每个用户都不同。 我尝试了hash_including方法如下: – stub_request(:post, “https://api.mailjet.com/v3/send”). with( body: hash_including({“FromEmail”: “abc@abc.com”,”FromName”: “abc”}), headers: { ‘Accept’=>’application/json’, ‘Accept-Encoding’=>’deflate’, ‘Authorization’=>’Basic YmY0NzdhZmYyMmZlMGQwMzAxNzYzNDNkODUwZTY1MzE6YmRhNmZmYzQ0Y2ZmNGM3MmZkZGNkMjUwODA5YWJhZjM=’, ‘Content-Length’=>’143’, ‘Content-Type’=>’application/json’, ‘Host’=>’api.mailjet.com’, ‘User-Agent’=>’mailjet-api-v3-ruby/1.5.4’ }). to_return(status: 200, body: “”, headers: […]

使用具有Rspec视图的Factory Girl

所以我想使用Rspec和Factory Girl测试一些视图,但我不知道如何正确地将工厂分配给视图。 我在网上看到的所有信息都使用了存根,虽然存根很好,但我想保持我的rspec内容有些一致。 我想使用工厂作为版本模型,并在渲染页面时将其与页面关联,但我考虑过的所有内容似乎都被打破了。 这是一个荒谬的例子: require ‘spec_helper’ describe “catalog/editions/rationale.html.haml” do before do @edition = Factory.create(:edition) assign(:editions, @edition) render :action => ‘rationale’, :id => @edition end context “GET rationale” do it “should not have the preamble to the United States constitution” do rendered.should_not contain(/We the People of the United States/) end end end 在这里我尝试改变渲染:action =>’rationale’,:id => […]

Capybara :: Webkit :: Driver的未定义方法accept_modal

在Rails应用程序中,我使用RSpec(使用Capybara Webkit)来测试Delete链接是否正常工作。 在我的Rails模板中,我有: 这是我的规格: require ‘rails_helper’ describe “Deleting a movie”, js: true do it “destroys the movie and shows the movie listing without the deleted movie” do movie = Movie.create(movie_attributes) visit movie_path(movie) page.accept_confirm do click_link ‘Delete’ end expect(current_path).to eq(movies_path) expect(page).not_to have_text(movie.title) end end 我收到错误: NoMethodError: undefined method `accept_modal’ for # 它使用正确的驱动程序(Webkit),但它似乎没有找到accept_modal (必须由page.accept_confirm )。 我正在使用: capybara […]

如何在页面顶部包含javascript_include_tag的javascript以进行水豚测试

我试图用capybara在spec测试中测试一个javascript类。 我有静态页面player.haml,我在其中使用javascript_include_tag加载javascript类定义。 它看起来像那样 .container = javascript_include_tag “../player” #player 我的测试看起来像那样 it ‘can autoplay a video’ do visit player_path video = Fabricate(:video) options = { autoplay: true, … } page.execute_script(“var options = ‘#{options}’; videoId = ‘#{video.id}’; var player = new Player(videoId, options);”) expect(page).to have_content(video.title) end 问题是javascript_include_tag包含页面底部的脚本和execute_script调用之后的脚本。 我在访问该页面后尝试了睡眠,但结果是一样的。 如何在使用execute_script之前加载类定义? 最终的瓶颈是具有播放器定义的文件是.coffee.erb。 我可以更改,因为它使用应用程序中的一些设置。

Ruby on Rails教程第10章练习RSpec失败

我正在研究Rails教程第10章中的练习,并通过练习让我确保管理员用户无法自行删除。 我最初的想法是简单地检查当前用户的id并将其与params [:id]进行比较,以确保它们不相等。 我在Users控制器中的销毁操作如下所示: def destroy if current_user.id == params[:id].to_i flash[:notice] = “You cannot delete yourself.” else User.find(params[:id]).destroy flash[:success] = “User destroyed.” end redirect_to users_path end 当我在应用程序中手动测试它时,这非常有效,但是我的RSpec测试中有3个失败并出现相同的“未定义方法’to_i’”错误(如下所示): 1) UsersController DELETE ‘destroy’ as an admin user should destory the user Failure/Error: delete :destroy, :id => @user NoMethodError: undefined method `to_i’ for # # ./app/controllers/users_controller.rb:48:in `destroy’ # […]

如何运行多个特定的RSpec测试?

我有一个巨大的测试套件(> 5000次测试)需要一个小时才能运行。 400失败了,我有一个失败的测试列表,如下所示: rspec ./spec/models/fullscreen_hidden_view_state_spec.rb:116 # FullscreenHiddenViewState showing a playlist on third element – slide has all the right links and properties rspec ./spec/features/fullscreen/play_spec.rb:59 # View case in fullscreen presention mode Courtesy section when viewing the cases discussion via the cases hidden share token the Add To button should be hidden rspec ./spec/features/cases/index_spec.rb:204 # finding […]

RSpec Rails登录filter

我最近开始使用我的Rails(3.0.8)应用程序使用rspec-rails(2.6.1)。 我已经习惯了Test :: Unit,而且我似乎无法为我的测试方法提供filter。 我喜欢尽可能保持DRY,所以我想设置一个filter,我可以在调用测试方法之前调用任何将作为Authlogic用户登录的测试方法。 我尝试使用spec_helper.rb中的RSpecfilter来完成此操作: config.before(:each, :login_as_admin => true) do post “/user_sessions/create”, :user_session => {:username => “admin”, :password => “admin”} end 然后我在相应的测试方法中使用它(在本例中为spec / controllers / admin_controller_spec.rb): require ‘spec_helper’ describe AdminController do describe “GET index” do it(“gives a 200 response when visited as an admin”, :login_as_admin => true) do get :index response.code.should eq(“200”) end end […]

为什么before_action:authorize以’错误的参数数’失败?

我已经和Devise一起成立了Pundit,以便对我的申请进行授权。 在我的一个控制器中,我有before_action :authorize 。 然后我进行了以下测试: describe SomeController do before(:each) do login_user(FactoryGirl.create(:user, :user_type => :admin)) end describe “GET index” do it “it retrieves the index” do something = FactoryGirl.create(:Something) get :index assigns(:something).should eq([something]) end end end 我收到错误: wrong number of arguments (0 for 1) 登录助手非常简单: module ControllerMacros def login_user(user) if user.nil? user = FactoryGirl.create(:user) end @request.env[“devise.mapping”] […]

NameError:未定义的局部变量或方法`app’

我创建的控制器规范失败,并显示以下错误消息: NameError: undefined local variable or method `app’ for \ # … # spec/controllers/sessions_conroller_spec.rb require ‘spec_helper’ describe SessionsController do before do @user = User.gen! end describe “#create” do context “when sending valid email and password” do it “renders a json hash …” do post :create, email: @user.email, password: @user.password expect(last_response.status).to eq(201) end end end describe […]