rspec

rspec before(:each) hook - conditionally apply

╄→гoц情女王★ 提交于 2019-12-25 04:29:15
问题 I have following in my rails_helper.rb : RSpec.configure do |config| # ... config.before(:each, type: :controller) do # SOMETHING end end I want to define directories, to which this SOMETHING will be applicable (in my case ONLY to files under spec/controllers/api directory). Any chance to achieve that? 回答1: You can use a more specialized name for your RSpec filter: RSpec.configure do |config| # ... config.before(:each, :subtype => :controllers_api) do # SOMETHING end end And then in your

How to check whether a variable is an instance of a module's subclass using rspec?

不羁岁月 提交于 2019-12-25 04:22:39
问题 I have a class structure that looks like this: module MyModule class MyOuterClass class MyInnerClass end end end I'm trying to make sure that a variable was correctly instantiated as a MyInnerClass using Rspec. printing the type of the class, it was MyModule::MyOuterClass::MyInnerClass. However, if I try to run the line expect{@instance_of_MyInnerClass}.to be_an_instance_of(MyModule::MyOuterClass::MyInnerClass) I get the error "You must pass an argument rather than a block to use the provided

rspec issue believed to be with gets.chomp call

浪子不回头ぞ 提交于 2019-12-25 04:13:13
问题 I am having trouble or am confused with an rspec error when running: rspec -fd game_spec (the above is only the beginning of a long eror message) When I run it without the -fd, it works... but it looks goofy... something is wrong. I start getting error messages when I use gets.chomp but I've looked at similar examples that use it. Does 'the_word' have to be set in initialize? Does gets.chomp or can it? I appreciate any help. Below is game file and its rspec so far. I do not want to change

Trouble on evaluating other model data

纵饮孤独 提交于 2019-12-25 04:06:20
问题 I am using Ruby on Rails 3.0.9 and RSpec 2. In my spec file I have code like the following: describe User do let(:authorizations) { Authorization.all.map(&:name) } it "should have a 'registered' value" do authorizations.should include("registered") end end When I run the above test I get: User should have a 'registered' value Failure/Error: authorizations.should include("registered") expected [] to include "registered" Diff: @@ -1,2 +1,2 @@ -registered +[] Is it possible to solve the above

Run code before before block

旧城冷巷雨未停 提交于 2019-12-25 03:58:13
问题 Rather than do this: subject { response } context 'when orphan' do before do post :create, asset: { parent_id: nil, uploaded_file: file } end it { should have_http_status 201 } it { should redirect_to_location '/' } it 'create a asset' do expect { post :create, asset: { parent_id: nil, uploaded_file: file } }.to change(Asset, :count).by(1) end end I want to be able to do this: subject { response } context 'when orphan' do before do post :create, asset: { parent_id: nil, uploaded_file: file }

Why rspec doesn't wait completion of previous command?

青春壹個敷衍的年華 提交于 2019-12-25 03:44:26
问题 My rails-app contain rspec-tests using Capybara poltergeist (which uses phantomjs). Frontend is Backbone.js. Some tests are sometimes passed, sometimes not. Debugging shows that the right content is not loaded at the time of inspection. If i add an delay, e.c. sleep (inspection_time = 1), then the test passes. describe 'Direct visit page / # / desk / page / [ID]' do before {sign_in user} it 'should contain page title' do visit "/#/desk/pages/#{page.id}" #sleep (inspection_time = 1) expect

Rspec checking for json response

杀马特。学长 韩版系。学妹 提交于 2019-12-25 03:24:38
问题 I am trying to create a mock object and i am checking whether my method receives the right param and expected result. Below is my spec . require 'spec_helper' describe User do let(:username) {"test@test.com"} let(:password) {"123"} let(:code) {"0"} context "when signing in" do it "should sign in" do user = double("user") expected_results = { "token": "123" } allow(user).to receive(:login).with({email: username, password: password, code: code}) .and_return(expected_results) expect(user.login)

How do you get rspec to output what it encountered rather than it “didn't find what it expected”?

余生长醉 提交于 2019-12-25 02:50:32
问题 I have been struggling to using ruby/rspec/capybara/devise to test my code. A simple test I am trying to write is for signing in. I have a valid user sign in and expect to see an h1 tag as defined in the following code: describe "Authentication" do subject { page } describe "with valid information" do let(:user) { FactoryGirl.create(:user) } before { sign_in_with user.email } it { should have_css('h1', text: "Welcome to the Test") } end end Problem is that I get this in return: 1)

FactoryGirl creating extra records with has_many association

拜拜、爱过 提交于 2019-12-25 02:22:35
问题 I'm running into an issue where FactoryGirl appears to be creating extra records with a has_many relationship. Given these models: class NextAction < ActiveRecord::Base has_many :next_actions_orders has_many :orders, through: :next_actions_orders end class NextActionsOrder < ActiveRecord::Base belongs_to :order belongs_to :next_action end class Order < ActiveRecord::Base has_many :next_actions_orders has_many :next_actions, through: :next_actions_orders end And these factories: FactoryGirl

Infamous AbstractController::ActionNotFound - Devise + Rails

五迷三道 提交于 2019-12-25 02:07:25
问题 So I have read how to solve this problem: RSpec Test of Custom Devise Session Controller Fails with AbstractController::ActionNotFound and http://lostincode.net/blog/testing-devise-controllers But under which file do I add these changes is my problem: Under the rspec folder for my registrations_controller I tried this before :each do request.env['devise.mapping'] = Devise.mappings[:user] end require 'spec_helper' describe RegistrationsController do describe "GET 'edit'" do it "should be