rspec

How do i set a request.referrer inside my RSpec?

北城余情 提交于 2019-12-31 17:51:30
问题 I am trying to avoid using my session[:referred_by] , and would like to use the request.referrer . However, my RSpec tests fail because the TestRequest does not store a request.referrer So I have to do the following in order for Rspec tests to work. Is there a way to make it better: referrer = request.referrer ? request.referrer : '/' redirect_to referrer, :alert => error_message 回答1: Mock it: specify 'foo' do controller.request.should_receive(:referer).and_return('http://example.com') # get

RSpec stub helper method in controller spec

江枫思渺然 提交于 2019-12-31 14:16:49
问题 Found similar questions but surprisingly none, that I've found, give a simple answer... Trying to stub a helper method in my controller spec; not quite sure what object would need to be doubled? Controller calls this method: #app/helpers/sessions_helper.rb def signed_in? current_user.present? end I'd like to stub it in spec to return true/false. 回答1: You can stub it from the controller spec: controller.stub!(:signed_in?).and_return(true) # emulate signed in user controller.stub!(:signed_in?)

Rspec: testing assignment of instance variable

谁说我不能喝 提交于 2019-12-31 11:02:11
问题 Using Rspec with Factory Girl. Trying to check out what data is being assigned in my controller (and test against it). Every post I've read says I should be able to get something out of assigns() but it keeps returning nill Controller def index @stickies = Sticky.where(:user_id => current_user.id) end Spec it "should assign stickies" do foo = assigns(:stickies) puts "foo = #{foo}" end Output foo = Am I using the wrong syntax? Is there a better way to do this? Thanks!! 回答1: You have to invoke

Rails 3.2, RSpec, Factory Girl : NameError: uninitialized constant Factory

社会主义新天地 提交于 2019-12-31 10:59:05
问题 Ive been following this introductory to Rails testing and Ive run into an issue I cant seem to find the solution to. Im very familiar with Rails but this is my first foray into testing. Anyhow, I have a very basic model test, not even fully implemented and when I try and run rspec spec/models/admin_spec.rb . I get the following error in the Admin has a valid factory line (full code below) Admin has a valid factory Failure/Error: Factory.create(:admin).should be_valid NameError: uninitialized

How to test exception raising in Rails/RSpec?

醉酒当歌 提交于 2019-12-31 10:38:09
问题 There is the following code: def index @car_types = car_brand.car_types end def car_brand CarBrand.find(params[:car_brand_id]) rescue ActiveRecord::RecordNotFound raise Errors::CarBrandNotFound.new end I want to test it through RSpec. My code is: it 'raises CarBrandNotFound exception' do get :index, car_brand_id: 0 expect(response).to raise_error(Errors::CarBrandNotFound) end CarBrand with id equaling 0 doesn't exist, therefore my controller code raises Errors::CarBrandNotFound, but my test

cucumber vs. RSpec

泪湿孤枕 提交于 2019-12-31 09:15:10
问题 I want to start diving into BDD. I have never used TDD before and am not sure if I should start by learning RSpec and then jump to Cucumber or just go straight to using Cucumber. I have been reading on the internet about both and it seems to me that Cucumber could be a 'replacement' for RSpec. Am I right or should be one used for certain things and the other one for others? 回答1: Yes, cucumber and rspec are both used for BDD. I personally prefer Cucumber, but some people find it offputting,

Understanding Rails 3's respond_with

半世苍凉 提交于 2019-12-31 09:03:12
问题 Utilizing ActionController's new respond_with method...how does it determine what to render when action (save) is successful and when it's not? I ask because I'm trying to get a scaffold generated spec (included below) to pass, if only so that I can understand it. The app is working fine but, oddly, it appears to be rendering /carriers (at least that's what the browser's URL says) when a validation fails. Yet, the spec is expecting "new" (and so am I, for that matter) but instead is receiving

Is there a python equivalent for RSpec to do TDD?

蹲街弑〆低调 提交于 2019-12-31 08:28:50
问题 I'm looking for a test framework like Ruby's RSpec to do test driven development in Python. The advantage of a framework like RSpec is that it offers a DSL that lends itself well to TDD. First you describe the test in english, and then you write the test, and when it fails you get a message saying what test failed with your nice description of what the test is trying to do. So far I've looked at PyTest and Nose. PyTest seems closer to ruby's MiniTest than RSpec. Instead of offering a DSL with

How can I check that a form field is prefilled correctly using capybara?

情到浓时终转凉″ 提交于 2019-12-31 07:57:38
问题 I have a field with a proper label that I can fill in with capybara without a problem: fill_in 'Your name', with: 'John' I'd like to check the value it has before filling it in and can't figure it out. If I add after the fill_in the following line: find_field('Your name').should have_content('John') That test fails, although the filling just before worked as I've verified by saving the page. What am I missing? 回答1: You can use an xpath query to check if there's an input element with a

Testing an RSpec controller action that can't be accessed directly

浪子不回头ぞ 提交于 2019-12-31 01:58:48
问题 I've got a controller that can't be accessed directly, in the traditional RESTful way, but rather only through a particular url. Normally I'm used to using get and post in my controller specs to call controller actions. Is there a way that I can exercise my controller by visiting a particular url? EDIT: Here is my route: Larzworld::Application.routes.draw do match '/auth/:provider/callback' => 'authentications#create' devise_for :users, :controllers => {:registrations => "registrations"} root