rspec-rails

undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1057fd428> error while trying to set up RSpec with Devise

时间秒杀一切 提交于 2019-12-12 10:47:26
问题 I've got a spec/controllers/add_to_carts_spec.rb : require 'spec_helper' describe CartItemsController do before (:each) do @user = Factory(:user) sign_in @user end describe "add stuff to the cart" do it "should add a product to the cart" do product = FactoryGirl.create(:product) visit products_path(product) save_and_open_page click_on('cart_item_submit') end end end and /spec/support/spec_helper.rb : # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] |

Rspec: add some header requests inside routing specs

天涯浪子 提交于 2019-12-12 10:32:50
问题 I'm working on a Rails application having a REST API in JSON format and versioned (according to this excellent Ryan's cast: http://railscasts.com/episodes/350-rest-api-versioning). For instance, there is a spec/requests spec: require 'spec_helper' describe "My Friends" do describe "GET /my/friends.json" do it "should get my_friends_path" do get v1_my_friends_path, {}, {'HTTP_ACCEPT' => 'application/vnd.myapp+json; level=1'} response.status.should be(401) end end end And it works well. But

Ruby on Rails App Testing with Rspec and Capybara

蹲街弑〆低调 提交于 2019-12-12 04:59:34
问题 when i create a feature test for my application get the following error: ActionController::RoutingError: No route matches [GET] "/example" My application uses subdomains and sub-applications(engines/modules) within these subdomains. Now when i set for Capybara the app_host or default_host through an feature_subdomain_helper like Capybara.app_host = "example.lvh.me" or Capybara.default_host = "example.lvh.me" and into my rails_helper.rb i add the following code line config.extend

Testing singular resource controller with RSpec

偶尔善良 提交于 2019-12-12 04:34:13
问题 I have defined a singular resource in my routes.rb which looks like this Rails.application.routes.draw do resource :dog, only: [:create], to: "dog#create", controller: "dog" end After that I've defined a controller with a create action like this class DogController < ApplicationController def create render json: {}, status: :ok end end And now I'm trying to test it out with RSpec like this require "rails_helper" describe DogController do it "works" do post :create, params: { foo: :bar } end

Rails controller rspecs fail with RoutingError for spec/controllers but pass for individual tests

房东的猫 提交于 2019-12-12 02:57:32
问题 My controller specs are passing when I run them one by one but are all failing when I run them together as controllers. $rspec spec/controllers 22) SubjectsController GET index assigns all subjects as @subjects Failure/Error: get :index, {} ActionController::RoutingError: No route matches {:controller=>"subjects"} # ./spec/controllers/subjects_controller_spec.rb:13:in `block (3 levels) in <top (required)>' My guess is it must be something to do with database cleaner but I'm not sure best way

Stub a controller helper method in a helper spec

萝らか妹 提交于 2019-12-12 02:46:48
问题 In my application_controller.rb : helper_method :current_brand def current_brand @brand ||= Brand.find_by_organization_id(current_user.organization_id) end In my helper something_helper.rb def brands return [] unless can? :read, Brand # current_brand is called end I am writing a spec for something_helper and wish to stub current_brand describe SomethingHelper do before :each do helper.stub!(:can?).and_return(true) # This stub works end it "does the extraordinary" do brand = Factory.create(

spec with guard, rails 3.1.1, and ruby 1.9.3 getting cannot load such file errors

*爱你&永不变心* 提交于 2019-12-12 01:49:27
问题 HI all I just recently installed rvm and all that good stuff on my macbook air and I started my rails 3.1.1 app with ruby 1.9.3. In my gem file I put down guard and rspec, now when I try and run rspec by itself or with guard I get errors thrown at me and I'm not sure why, the output I get when I run is such, phantom:tasks maxmarze$ guard Please install rb-fsevent gem for Mac OSX FSEvents support Using polling (Please help us to support your system better than that). Please install growl

FactoryGirl creates incomplete model

旧街凉风 提交于 2019-12-11 19:42:24
问题 Assume that I have a city model where: class city field :full_name, type: String # San Francisco, CA, United States field :_id, type: String, overwrite: true, default: ->{ full_name } end Assume that I have a factory defined in /spec/factories/cities.rb : FactoryGirl.define do factory :city do full_name 'San Francisco, CA, United States' end end Running the following code in one of the specs: city_attrs = { full_name: 'San Francisco, CA, United States' } City.create! city_attrs => #<City _id:

Using Rails default exception translation in specs

北城以北 提交于 2019-12-11 13:57:22
问题 My controller is throwing ActiveRecord::RecordNotFound which is what to be expected to be translated into 404. Now I want to test this behaviour in my controller spec, but it gets exception rather than response_code equal to 404. How to make it get this code instead? 回答1: When Rails raise a ActiveRecord::RecordNotFound it simply telling you that ActiveRecord was not able to find a resource in your database (usually using find ). It is your responsability to catch the exception and to do

Undefined Method 'NameOfField' for #<Model:0x000…>

若如初见. 提交于 2019-12-11 13:42:01
问题 I'm totally stumped on this error. Would really appreciate some help :). To reproduce the error, you can pull the program from https://github.com/WaleyChen/twitter_clone. Then run 'bundle exec rspec spec/'. I have an rspec test for my controller defined as: require 'spec_helper' describe FrontpageController do render_views # render the views inside the controller tests, so not just test the actions describe "GET 'frontpage'" do it "should be successful" do get 'frontpage' response.should be