rspec

RSpec Rails Login Filter

最后都变了- 提交于 2020-01-04 14:14:19
问题 I recently switched started using rspec-rails(2.6.1) with my Rails(3.0.8) app. I'm used to Test::Unit, and I can't seem to get a filter working for my test methods. I like to keep things as DRY as possible, so I'd like to set up a filter that I can call on any test method that will login as an Authlogic user before the test method is called. I tried accomplishing this by using an RSpec filter in spec_helper.rb: config.before(:each, :login_as_admin => true) do post "/user_sessions/create",

How to pass objects with stubbed methods in it_behaves_like to shared_examples_for block in rspec?

∥☆過路亽.° 提交于 2020-01-04 13:49:37
问题 I have a spec like describe MyClass do it_behaves_like SharedClass, MyClass.new end and in my shared example spec, I have shared_examples_for SharedClass do |instance| before do instance.some_my_class_method = double end # some specs here end There are few methods in MyClass instance that I cannot stub in the shared_examples_for block, so I want to stub them before passing it in it_behaves_like statement. like, describe MyClass do before do @instance = MyClass.new @instance.stub(:my_class

How to pass objects with stubbed methods in it_behaves_like to shared_examples_for block in rspec?

若如初见. 提交于 2020-01-04 13:49:07
问题 I have a spec like describe MyClass do it_behaves_like SharedClass, MyClass.new end and in my shared example spec, I have shared_examples_for SharedClass do |instance| before do instance.some_my_class_method = double end # some specs here end There are few methods in MyClass instance that I cannot stub in the shared_examples_for block, so I want to stub them before passing it in it_behaves_like statement. like, describe MyClass do before do @instance = MyClass.new @instance.stub(:my_class

How to write an RSpec test for Ryan Bates' helper from screencast #228?

白昼怎懂夜的黑 提交于 2020-01-04 13:37:21
问题 I followed along with Ryan Bates' tutorial on sortable table columns. I attempted to write a spec for the ApplicationHelper , but the #link_to method fails. Here is my spec: require "spec_helper" describe ApplicationHelper, type: :helper do it "generates sortable links" do helper.sortable("books") #just testing out, w/o any assertions... this fails end end Here is the output from running the spec: 1) ApplicationHelper generates sortable links Failure/Error: helper.sortable("books") #just

Testing Rails module without loading ActiveRecord

孤者浪人 提交于 2020-01-04 11:10:08
问题 I'm working on a Rails app which I am testing with RSpec. After a lot of waiting for tests to finish, I've followed Gary Bernhardt's advice and this informative post by Paul Annesley and setup a tiered spec_helper where I only load what parts of Rails I absolutely need, to keep test times down, and extract functionality into separate modules which I test in isolation. This is working up to a point. The problem is that I have a module (extending ActiveSupport::Concern ) with instance and class

Ruby TypeError: no implicit conversion of String into Array

*爱你&永不变心* 提交于 2020-01-04 09:19:12
问题 I have a test that returns TypeError: no impliciit conversion of String into Array , when it hits a certain section of my code. If I run the code outside of rspec it runs just fine, so I'm not sure why this is happening. require 'spec_helper' require 'digital_ocean_size_list' describe Chef::Knife::DigitalOceanSizeList do subject { Chef::Knife::DigitalOceanSizeList.new } let(:access_token) { 'FAKE_ACCESS_TOKEN' } before :each do Chef::Knife::DigitalOceanSizeList.load_deps Chef::Config['knife']

How to test a multi-threaded TCPServer with rspec

坚强是说给别人听的谎言 提交于 2020-01-04 06:10:51
问题 I've written a really simple date server: require 'socket' s = TCPServer.new 3939 while (conn = s.accept) Thread.new(conn) do |c| c.print "Enter your name: " name = c.gets.chomp c.puts "Hi #{name}, the date is..." c.print `date` c.close end end A user connects, a thread is spawned, they enter their name, the date is returned. Simple. I'm wondering about how I would go abouts testing something like this in rspec. Some ideas that I've had: 1.) Use VCR to record a server connection and Timecop

How to test a multi-threaded TCPServer with rspec

◇◆丶佛笑我妖孽 提交于 2020-01-04 06:10:04
问题 I've written a really simple date server: require 'socket' s = TCPServer.new 3939 while (conn = s.accept) Thread.new(conn) do |c| c.print "Enter your name: " name = c.gets.chomp c.puts "Hi #{name}, the date is..." c.print `date` c.close end end A user connects, a thread is spawned, they enter their name, the date is returned. Simple. I'm wondering about how I would go abouts testing something like this in rspec. Some ideas that I've had: 1.) Use VCR to record a server connection and Timecop

view.stub in rails partial rspec gives 'undefined method view_context'

浪尽此生 提交于 2020-01-03 17:12:59
问题 using Rails 3.2.11 I have a couple of view rspec tests where I need to stub the 'current_user' call. I've used this successfully in a regular view test like so: require 'spec_helper' describe "projects/_my_project.html.erb" do before(:each) do @client = FactoryGirl.create(:user) view.stub(:current_user) { @client } end describe "new proposals notice" do it "does not display new_propsals if in state posted and clicked after last submitted" do @my_project = FactoryGirl.build(:project, status:

How can I test that no attribute on a model has been modified using rspec?

六月ゝ 毕业季﹏ 提交于 2020-01-03 16:54:12
问题 I want to check if no attributes on an ActiveRecord object have been modified. Currently I'm doing this: prev_attr = obj.attributes <- this will give me back a Hash with attr name and attr value And then, later, I grab the attributes again and compare the 2 hashes. Is there another way? 回答1: You should just be able to use equality matchers - does this not work for you? a = { :test => "a" } b = { :test => "b" } $ a == b => false b = { :test => "a" } $ a == b => true Or to use your example: