rspec2

Repeated test descriptions with RSpec for every user role

落花浮王杯 提交于 2019-12-04 11:25:47
Creating some controller tests with RSpec, I find myself repeating several test cases for every possible user role. For example describe "GET 'index'" do context "for admin user" do login_user("admin") it "has the right title" do response.should have_selector("title", :content => "the title") end end context "for regular user" do login_user("user") it "has the right title" do response.should have_selector("title", :content => "the title") end end end This is a simple example just to make my point, but I have a lot of tests that are repeated... Of course there are also some tests that are

How do I make config.exceptions_app work with rspec

╄→尐↘猪︶ㄣ 提交于 2019-12-04 08:20:21
In my Rails 3.2 app, I'm trying to use config.exceptions_app to route exceptions through the routing table to render error-specific pages (especially one for 401 Forbidden). Here's what I've got so far for configuration: # application.rb config.action_dispatch.rescue_responses.merge!('Error::Forbidden' => :forbidden) config.exceptions_app = ->(env) { ErrorsController.action(:show).call(env) } # development.rb config.consider_all_requests_local = false # test.rb config.consider_all_requests_local = false And now the meat of the matter: module Error class Forbidden < StandardError end end class

Rails 3.1, RSpec: testing model validations

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 07:34:04
问题 I have started my journey with TDD in Rails and have run into a small issue regarding tests for model validations that I can't seem to find a solution to. Let's say I have a User model, class User < ActiveRecord::Base validates :username, :presence => true end and a simple test it "should require a username" do User.new(:username => "").should_not be_valid end This correctly tests the presence validation, but what if I want to be more specific? For example, testing full_messages on the errors

Rspec testing of counter_cache column's returning 0

左心房为你撑大大i 提交于 2019-12-04 04:22:24
For days now I have been trying to get to the bottom of what seam to be something that should be very easy to do... I am however still very new to the world of rails and ruby and I just cant work this one out... :p Anyway the problem I am having is that I have a number of :counter_cache columns in my model's, which are all working quite nicely when testing them manually. However I am wanting to do the TDD thing and I cant seam to test them in rspec for some unknown reason?? Anyway here is an example of my model's (User's, comments & Media): class User < ActiveRecord::Base has_many :comments

session object in rspec integration test

雨燕双飞 提交于 2019-12-04 03:44:54
I am using rspec and capybara for integration testing. Is their a way to make session objects in request specs? I have a view page in which I use a session object to check its value to display selective content. The problem am facing is that I cannot create a session object in request spec. Here is an example of the view: <% if session[:role] == "Role" %> ---content--- <% else %> --content-- <% end %> And inside my request spec session[:role] = "Role" visit my_path But it throws me an error "undefined method `session' for nil:NilClass". I also tried looking into creating session objects of

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

半城伤御伤魂 提交于 2019-12-03 23:39:30
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_helper' describe "Sessions" do it 'allows user to login' do #line one user = Factory(:user) #For SO, this

Rspec testing redirect_to :back

社会主义新天地 提交于 2019-12-03 18:57:15
问题 How do you test redirect_to :back in rspec? I get ActionController::RedirectBackError : No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"] . How do I go about setting the HTTP_REFERER in my test? 回答1: Using RSpec, you can set the referer in a before block. When I tried to set the referer directly in the test, it didn't seem to work no matter where I put it, but the

rspec test result from csv.read mocking file

微笑、不失礼 提交于 2019-12-03 16:46:23
问题 I'm using ruby 1.9 and I'm trying to do BDD. My first test 'should read in the csv' works, but the second where I require a file object to be mocked doesn't. Here is my model spec: require 'spec_helper' describe Person do describe "Importing data" do let(:person) { Person.new } let(:data) { "title\tsurname\tfirstname\t\rtitle2\tsurname2\tfirstname2\t\r"} let(:result) {[["title","surname","firstname"],["title2","surname2","firstname2"]] } it "should read in the csv" do CSV.should_receive(:read

rspec/capybara: how to simulate incoming POST requests? (rack-test won't work)

折月煮酒 提交于 2019-12-03 16:45:19
I need to receive incoming emails as multipart-formdata via a POST request from Cloudmailin. The POST resembles the following: Parameters: {"to"=>"<email@exmaple.comt>", "from"=>"whomever@example", "subject"=>"my awesome subject line.... Actually, receiving and parsing emails is super easy because the email is just posted as params: params[:to], params[:from], etc. However, how do I simulate this POST request in rails? I built a dummy rails app to test out Cloudmailin, so I have an actual request. However, it's a 6k character file, so I'd like to load this file as the parameters of the POST

stub_chain together with should_receive

浪子不回头ぞ 提交于 2019-12-03 16:31:41
问题 I am trying to test if in a method calling chain one of the methods get a specific parameter. In the below code for example MyModel must receive the parameter 0 for the method offset . Unfortunately the code below does not work. It seems it is not possible to mix should_receive and stub_chain. How could I solve this? I am using RSpec 2. MyModel.should_receive(:offset).with(0).stub_chain(:tag_counts, :offset, :limit, :order).and_return([]) # does not work! The code I am trying to test: tags =