rspec-rails

How to disable a before_action in a controller spec?

社会主义新天地 提交于 2019-12-23 12:02:05
问题 I've employed the use of this in my controller spec: controller.class.skip_before_action Specifically, in this case: controller.class.skip_before_action :require_authorisation_to_view_materials MaterialsController: class MaterialsController < ApplicationController before_action :set_material, only: [:show, :edit, :update, :destroy] before_action :require_authorisation_to_view_materials, only: [:index, :show] def require_authorisation_to_view_materials # For Materials Page unless user_signed

Ruby/Rails/Rspec - ActiveRecord::AssociationTypeMismatch:

ぐ巨炮叔叔 提交于 2019-12-23 03:45:21
问题 OK, here's my rspec code ... 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 Now the output from this is: spec: @attr = {:greeting=>"Lorem ipsum", :recipient=>#<User id: 2, first_name: "Example", last_name: "User", email: "recipient@example.com", created_at: "2011-12-22

render_views returning NoMethodError

大憨熊 提交于 2019-12-23 02:39:27
问题 I'm currently going through the Ruby on Rails Tutorial by Michael Hartl, and in Section 3.2.2 of the tutorial, he talks about using RSpec to test for the titles of the page. My tests (which I wrote myself, following his tutorial) kept failing the title tests (it can find the tag, but the title is always a blank string), and some searching on Google told me I needed to include the function render_views to pass the tests. My problem is that no matter what I try, RSpec returns a NoMethodError

Rspec rendering text

[亡魂溺海] 提交于 2019-12-22 03:57:42
问题 I have this Code if @temp_user.save sign_in(:user, @temp_user) render text: "OK" else render text: render_to_string(:partial => "errors") end and I try verify with rspec the render "OK" this is my actual spec: it "render text OK" do post :create, {:agent => valid_attributes} # response.should have_content("OK") response.should render_template(:text => "OK") end but this spec respond 0 failures always, even when I put "OKI" in place "OK" anyone have one suggestion for that? 回答1: If you are

Access to request object in request specs

谁说我不能喝 提交于 2019-12-21 09:03:15
问题 How can I set the request headers before doing the request on a request spec? I'm moving controller specs to request specs on my API using Rails. One thing I'm stuck on is that I have no access to a request object to allow the requests. On my controller specs, I have access to a method I created which signs a specific user: def sign_in(user) token = user.api_keys.first.token # note the request object being used in the next line request.env["HTTP_AUTHORIZATION"] = ActionController:

How to call a rake task in rspec

巧了我就是萌 提交于 2019-12-21 08:45:51
问题 I am trying to invoke a rake task in in my rspec. require "rake" rake = Rake::Application.new Rake.application = rake rake.init rake.load_rakefile rake['rake my:task'].invoke But i am getting error Failure/Error: rake['rake db:migrate'].invoke RuntimeError: Don't know how to build task 'rake db:migrate' Does anyone have a idea how we can invoke rake task in rspec code. Any help would be highly appreciated. 回答1: To pass in the arguments in square brackets to invoke : rake sim:manual_review

How to call a rake task in rspec

99封情书 提交于 2019-12-21 08:45:10
问题 I am trying to invoke a rake task in in my rspec. require "rake" rake = Rake::Application.new Rake.application = rake rake.init rake.load_rakefile rake['rake my:task'].invoke But i am getting error Failure/Error: rake['rake db:migrate'].invoke RuntimeError: Don't know how to build task 'rake db:migrate' Does anyone have a idea how we can invoke rake task in rspec code. Any help would be highly appreciated. 回答1: To pass in the arguments in square brackets to invoke : rake sim:manual_review

Factory Girl / Capybara deleting records from database mid-test?

时光毁灭记忆、已成空白 提交于 2019-12-21 07:09:36
问题 Working with RSpec & Capybara, I'm getting an interesting test failure mode which goes away with a few subtle rearrangements of lines in the test case...stuff that shouldn't matter. I'm developing my own authentication system. It is currently working and I can login/out with the browser and the session works etc etc. However, trying to test this is failing. Something is going on that I don't quite understand, which seems to depend on the order of (seemingly) unrelated calls. require 'spec

Mocking file uploads in Rails 3.1 controller tests

爷,独闯天下 提交于 2019-12-21 04:12:32
问题 My controller accesses the tempfile attribute of an uploaded file and passes it to another mocked component. My test code has @file = mock(Object) @file.stub_chain(:tempfile, :path).and_return('thefile.zip') # ... post :create, :file => @file and the controller code calls params[:file].tempfile.path . After upgrading from Rails 3.0 to 3.1, the above line started failing with undefined method `tempfile' for "#[RSpec::Mocks::Mock:0x2b0d9a0 @name=Object]":String That is, Rails 3.1 converted

Why is this rspec request spec not updating the model?

偶尔善良 提交于 2019-12-20 21:00:52
问题 I have a requests spec for interactions with the User model. I want to make sure that Users with the Admin role can create/edit/destroy Users. I'm having a problem right now where the Edit action does not update the user. Everything works properly when I manually go through the actions on the site itself, but the tests fail to update the user. Here's my spec: it 'edits a user' do @user = FactoryGirl.create(:user) visit new_user_session_path unless current_path == new_user_session_path fill_in