From sahmed1020 at gmail.com Thu Mar 1 05:04:00 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Thu, 1 Mar 2012 00:04:00 -0500 Subject: [rspec-users] issues when trying to select an option in a drop down list Message-ID: My html looks like: My requests spec looks like: before do select '7', :from => "account[car_type]" fill_in .. .. end I get the error message: Failure/Error: select '7', :from => "account[car_type]" Capybara::ElementNotFound: cannot select option, no option with text '7' in select box 'account[car_type]' # (eval):2:in `select' # ./spec/requests/account_pages_spec.rb:13:in `block (4 levels) in ' I also tried: select 'Honda', :from => "account[car_type]" And got the same type of error. What am I doing wrong here? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sahmed1020 at gmail.com Thu Mar 1 05:19:18 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Thu, 1 Mar 2012 00:19:18 -0500 Subject: [rspec-users] tests using factorygirl Message-ID: I'm trying to use factory for a test case, but the model I am creating via the factory depends on another model, how do you setup a factory like this? Model Account has a location_id attribute (where Location is another model) So how do I set it? FactoryGirl..... do location_id ????????/ end -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben at benatkin.com Thu Mar 1 05:56:12 2012 From: ben at benatkin.com (Ben Atkin) Date: Wed, 29 Feb 2012 22:56:12 -0700 Subject: [rspec-users] issues when trying to select an option in a drop down list In-Reply-To: References: Message-ID: >From reading the error and the docs, it sounds like the last one: select 'Honda', :from => "account[car_type]" ...should work. The one thing I can think of that might keep it from working would be not being scoped to an area that includes the select box. Perhaps you could install pry and add binding.pry above that line, and then run it, and then poke around in the repl and see if you've got the right context. You did make the request to get that page, right? Ben On Wed, Feb 29, 2012 at 10:04 PM, S Ahmed wrote: > My html looks like: > > > My requests spec looks like: > > before do > select '7', :from => "account[car_type]" > fill_in .. > .. > > end > > I get the error message: > > Failure/Error: select '7', :from => "account[car_type]" > Capybara::ElementNotFound: > cannot select option, no option with text '7' in select box > 'account[car_type]' > # (eval):2:in `select' > # ./spec/requests/account_pages_spec.rb:13:in `block (4 levels) in > ' > > > I also tried: > > select 'Honda', :from => "account[car_type]" > > And got the same type of error. > > What am I doing wrong here? > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben at benatkin.com Thu Mar 1 06:02:43 2012 From: ben at benatkin.com (Ben Atkin) Date: Wed, 29 Feb 2012 23:02:43 -0700 Subject: [rspec-users] tests using factorygirl In-Reply-To: References: Message-ID: Here is the relevant documentation: http://rdoc.info/github/thoughtbot/factory_girl/file/GETTING_STARTED.md#Associations Or if it already exists you might try: FactoryGirl.define :account do location { Location.where("city = ?", 'Austin').first } end (The code in the block just needs to return a single Location, and since it's run within a block it's delayed until the spec is run, rather than at load time.) Ben On Wed, Feb 29, 2012 at 10:19 PM, S Ahmed wrote: > I'm trying to use factory for a test case, but the model I am creating via > the factory depends on another model, how do you setup a factory like this? > > Model Account has a location_id attribute (where Location is another model) > > So how do I set it? > > FactoryGirl..... do > > location_id ????????/ > > end > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From julian at leviston.net Thu Mar 1 05:35:51 2012 From: julian at leviston.net (Julian Leviston) Date: Thu, 1 Mar 2012 16:35:51 +1100 Subject: [rspec-users] tests using factorygirl In-Reply-To: References: Message-ID: <56EBA3D0-8908-48AC-88F7-42BC2DC7A497@leviston.net> Nah I had this issue, too... use this FactoryGirl.... do assocation :location, etc. (look it up) end On 01/03/2012, at 4:19 PM, S Ahmed wrote: > I'm trying to use factory for a test case, but the model I am creating via the factory depends on another model, how do you setup a factory like this? > > Model Account has a location_id attribute (where Location is another model) > > So how do I set it? > > FactoryGirl..... do > > location_id ????????/ > > end > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From lists at ruby-forum.com Thu Mar 1 10:00:56 2012 From: lists at ruby-forum.com (bernhard stoecker) Date: Thu, 01 Mar 2012 11:00:56 +0100 Subject: [rspec-users] using fakeweb only if flag is given Message-ID: <08c71a357c8dbad3d7a04bb161df9320@ruby-forum.com> Hi, I'm using the gem fakeweb to simulate the comunication between my ruby-app and different other apps when running my rspecs. The advantage is, that the specs work much faster and it is not nessessary that the other apps are currently avalable. But for testing the integration of my app, it is of course nessessary to check weather my component is compatible with the others. What im searching is something like a flag to the comment bundle exec rspec spec e.g. bundle exec rspec spec --fake do decide to run fakeweb or not. Does something like this exist and if so how can I realize this. Best regards Bernhard -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Thu Mar 1 11:51:12 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 1 Mar 2012 05:51:12 -0600 Subject: [rspec-users] using fakeweb only if flag is given In-Reply-To: <08c71a357c8dbad3d7a04bb161df9320@ruby-forum.com> References: <08c71a357c8dbad3d7a04bb161df9320@ruby-forum.com> Message-ID: <789EBB4A-2858-41DA-8E40-3D28897604B2@gmail.com> On Mar 1, 2012, at 4:00 AM, bernhard stoecker wrote: > Hi, > > I'm using the gem fakeweb to simulate the comunication between my > ruby-app and different other apps when running my rspecs. The advantage > is, that the specs work much faster and it is not nessessary that the > other apps are currently avalable. But for testing the integration of my > app, it is of course nessessary to check weather my component is > compatible with the others. What im searching is something like a flag > to the comment bundle exec rspec spec e.g. bundle exec rspec spec --fake > do decide to run fakeweb or not. Does something like this exist and if > so how can I realize this. You could use tags for this: # CLI rspec --tags allow_net_connect # spec/spec_helper.rb RSpec.configure do |c| c.treat_symbols_as_metadata_keys_with_true_values = true end # spec describe "integration with real service", :allow_web_connect do # ... end HTH, David From sahmed1020 at gmail.com Thu Mar 1 17:19:53 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Thu, 1 Mar 2012 12:19:53 -0500 Subject: [rspec-users] issues when trying to select an option in a drop down list In-Reply-To: References: Message-ID: Yes I made the request, my test case was working fine with all my other fields. I just added the dropdown list and updated the test case, and it can't seem to find the element. On Thu, Mar 1, 2012 at 12:56 AM, Ben Atkin wrote: > >From reading the error and the docs, it sounds like the last one: > > select 'Honda', :from => "account[car_type]" > > ...should work. The one thing I can think of that might keep it from > working would be not being scoped to an area that includes the select box. > Perhaps you could install pry and add binding.pry above that line, and then > run it, and then poke around in the repl and see if you've got the right > context. You did make the request to get that page, right? > > Ben > > On Wed, Feb 29, 2012 at 10:04 PM, S Ahmed wrote: > >> My html looks like: >> >> >> My requests spec looks like: >> >> before do >> select '7', :from => "account[car_type]" >> fill_in .. >> .. >> >> end >> >> I get the error message: >> >> Failure/Error: select '7', :from => "account[car_type]" >> Capybara::ElementNotFound: >> cannot select option, no option with text '7' in select box >> 'account[car_type]' >> # (eval):2:in `select' >> # ./spec/requests/account_pages_spec.rb:13:in `block (4 levels) in >> ' >> >> >> I also tried: >> >> select 'Honda', :from => "account[car_type]" >> >> And got the same type of error. >> >> What am I doing wrong here? >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fourcatrails at gmail.com Thu Mar 1 16:43:43 2012 From: fourcatrails at gmail.com (Mike Kim) Date: Thu, 1 Mar 2012 08:43:43 -0800 (PST) Subject: [rspec-users] Testing selector order Message-ID: <644b2af7-1798-4179-be92-5f60c2083605@b1g2000yqb.googlegroups.com> Could someone point me to a reference that explains how I would test the order of elements on an HTML page? For example, in RSpec I would like to verify that the content of one

element appears before the content of another. response.should have_selector("p", :content => "Should appear first") response.should have_selector("p", :content => "Should appear second") The two lines above test that the 2 contents appear, but I don't know how to test that the first content appears before the second. Thanks Mike From apremdas at gmail.com Thu Mar 1 21:31:52 2012 From: apremdas at gmail.com (Andrew Premdas) Date: Thu, 1 Mar 2012 21:31:52 +0000 Subject: [rspec-users] Testing selector order In-Reply-To: <644b2af7-1798-4179-be92-5f60c2083605@b1g2000yqb.googlegroups.com> References: <644b2af7-1798-4179-be92-5f60c2083605@b1g2000yqb.googlegroups.com> Message-ID: On 1 March 2012 16:43, Mike Kim wrote: > Could someone point me to a reference that explains how I would test > the order of elements on an HTML page? > > For example, in RSpec I would like to verify that the content of one >

element appears before the content of another. > > response.should have_selector("p", :content => "Should appear first") > response.should have_selector("p", :content => "Should appear second") > > The two lines above test that the 2 contents appear, but I don't know > how to test that the first content appears before the second. > > Thanks > > Mike > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > Have a look at sibling in x-path specification to do it correctly. As a hack Capybara's all method in practice will return the paragraphs in order (but this isn't guaranteed by specification). HTH Andrew ------------------------ Andrew Premdas blog.andrew.premdas.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From sahmed1020 at gmail.com Fri Mar 2 03:33:45 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Thu, 1 Mar 2012 22:33:45 -0500 Subject: [rspec-users] can I output the html of the page after calling click_button? Message-ID: I am not sure why my form submission is failing (request test). I am calling: # form is filled in a 'before do' block. it "should create an account" do expect { click_button "Create" }.to change(Account, :count).by(1) end Could I somehow view the HTML after the click_button call? Manually submitting the form works fine for me, so I'm confused. -------------- next part -------------- An HTML attachment was scrubbed... URL: From apremdas at gmail.com Fri Mar 2 11:23:44 2012 From: apremdas at gmail.com (Andrew Premdas) Date: Fri, 2 Mar 2012 11:23:44 +0000 Subject: [rspec-users] can I output the html of the page after calling click_button? In-Reply-To: References: Message-ID: see below On 2 March 2012 03:33, S Ahmed wrote: > I am not sure why my form submission is failing (request test). > > I am calling: > > # form is filled in a 'before do' block. > > it "should create an account" do > expect { click_button "Create" }.to change(Account, :count).by(1) > put a debugger statement here debugger 1 # so the debugger doesn't drop into rspec end > > Could I somehow view the HTML after the click_button call? > > Manually submitting the form works fine for me, so I'm confused. > > Now you can 1) Look at the html directly e.g. > page.body 2) Output the page.body in a pretty way perhaps using Coderay 3) Use irb to do all sorts of other thing Alternatively use capybara' save_and_open_page HTH Andrew -------------- next part -------------- An HTML attachment was scrubbed... URL: From sahmed1020 at gmail.com Sat Mar 3 19:35:52 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Sat, 3 Mar 2012 14:35:52 -0500 Subject: [rspec-users] mimicking a logged in user Message-ID: I'm testing my controller, and confirming that a logged-in user can view the page. I have a session_helper that does this: def signed_in? !current_user.nil? end And the current_user is set with: def user_from_remember_token remember_token = cookies[:remember_token] User.find_by_remember_token(remember_token) unless remember_token.nil? end So in my controller, I need to fake a login somehow, how should I go about doing this? I'm not sure if this is the best place for it, but so far what I tried was creating a method in my spec_helper's config block: def test_sign_in(user) controller.sign_in(user) end (I get my user object from factorygirl). So my controller_spec looks like: describe "index" do before(:each) do @user = Factory(:user) test_sign_in(@user) end it "should be successful" do get 'index' controller.current_user.should == @user response.should be_success end end It is failing with current_user being nil. Should I just somehow stub the call to 'user_from_remember_token' to return the factorygirl user? How can I do this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at ruby-forum.com Sat Mar 3 21:55:55 2012 From: lists at ruby-forum.com (Greg C.) Date: Sat, 03 Mar 2012 22:55:55 +0100 Subject: [rspec-users] what RSpec approach could I use for this. Message-ID: <6154714fb3078c63792ba09e24c7190f@ruby-forum.com> Background: So I have roughly: class A def calculate_input_datetimes # do stuff to calculate datetimes - then for each one identified process_datetimes(my_datetime_start, my_datetime_end) end def process_datetimes(input_datetime_start, input_datetime_end) # do stuff end end So: * I want to test that calculate_input_datetimes algorithms are working and calculating the correct datetimes to pass to process_datetimes * I know I can STUB out process_datetimes so that it's code won't be involved in the test QUESTION: How can I setup the rspec test however so I can specifically test that the correct datestimes were attempted to be passed over to process_datetimes, So for a given spec test that process_datetimes was called three (3) times say with the following parameters passed: * 2012-03-03T00:00:00+00:00, 2012-03-09T23:59:59+00:00 * 2012-03-10T00:00:00+00:00, 2012-03-16T23:59:59+00:00 * 2012-03-17T00:00:00+00:00, 2012-03-23T23:59:59+00:00 thanks -- Posted via http://www.ruby-forum.com/. From jko170 at gmail.com Sat Mar 3 22:21:49 2012 From: jko170 at gmail.com (Justin Ko) Date: Sat, 3 Mar 2012 15:21:49 -0700 Subject: [rspec-users] mimicking a logged in user In-Reply-To: References: Message-ID: <97674CF8-C8AA-4C0C-A49F-2AB2EB4225DB@gmail.com> On Mar 3, 2012, at 12:35 PM, S Ahmed wrote: > I'm testing my controller, and confirming that a logged-in user can view the page. > > I have a session_helper that does this: > > def signed_in? > !current_user.nil? > end > > And the current_user is set with: > > def user_from_remember_token > remember_token = cookies[:remember_token] > User.find_by_remember_token(remember_token) unless remember_token.nil? > end > > > So in my controller, I need to fake a login somehow, how should I go about doing this? > > I'm not sure if this is the best place for it, but so far what I tried was creating a method in my spec_helper's config block: > > def test_sign_in(user) > controller.sign_in(user) > end > > (I get my user object from factorygirl). > > So my controller_spec looks like: > > > describe "index" do > before(:each) do > @user = Factory(:user) > test_sign_in(@user) > end > > it "should be successful" do > get 'index' > controller.current_user.should == @user > response.should be_success > end > end > > > It is failing with current_user being nil. > > Should I just somehow stub the call to 'user_from_remember_token' to return the factorygirl user? > How can I do this? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Just set the :remember_token cookie: https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/cookies From jko170 at gmail.com Sat Mar 3 22:29:26 2012 From: jko170 at gmail.com (Justin Ko) Date: Sat, 3 Mar 2012 15:29:26 -0700 Subject: [rspec-users] what RSpec approach could I use for this. In-Reply-To: <6154714fb3078c63792ba09e24c7190f@ruby-forum.com> References: <6154714fb3078c63792ba09e24c7190f@ruby-forum.com> Message-ID: On Mar 3, 2012, at 2:55 PM, Greg C. wrote: > Background: So I have roughly: > > class A > def calculate_input_datetimes > # do stuff to calculate datetimes - then for each one identified > process_datetimes(my_datetime_start, my_datetime_end) > end > > def process_datetimes(input_datetime_start, input_datetime_end) > # do stuff > end > end > > So: > * I want to test that calculate_input_datetimes algorithms are working > and calculating the correct datetimes to pass to process_datetimes > * I know I can STUB out process_datetimes so that it's code won't be > involved in the test > > QUESTION: How can I setup the rspec test however so I can specifically > test that the correct datestimes were attempted to be passed over to > process_datetimes, So for a given spec test that process_datetimes was > called three (3) times say with the following parameters passed: > > * 2012-03-03T00:00:00+00:00, 2012-03-09T23:59:59+00:00 > * 2012-03-10T00:00:00+00:00, 2012-03-16T23:59:59+00:00 > * 2012-03-17T00:00:00+00:00, 2012-03-23T23:59:59+00:00 > > > thanks > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Setup 3 individual expectations: a_instance.should_receive(:process_datetimes).with(input_datetime_start_1, datetime_end_1).once.ordered a_instance.should_receive(:process_datetimes).with(input_datetime_start_2, datetime_end_2).once.ordered Your 2 methods should be tested in isolation (unless #process_datetimes is private). So for #calculate_input_datetimes, just ensure that #process_datetimes is called with the correct arguments. The, test #processs_datetimes in isolation. From sahmed1020 at gmail.com Sun Mar 4 04:04:58 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Sat, 3 Mar 2012 23:04:58 -0500 Subject: [rspec-users] mimicking a logged in user In-Reply-To: <97674CF8-C8AA-4C0C-A49F-2AB2EB4225DB@gmail.com> References: <97674CF8-C8AA-4C0C-A49F-2AB2EB4225DB@gmail.com> Message-ID: thanks. but I have to stub the call that hits the database also. how can I wrap this up in a function so I can use it everywhere in my tests where I require a logged in user? where should I put this? in spec_helper? On Sat, Mar 3, 2012 at 5:21 PM, Justin Ko wrote: > > On Mar 3, 2012, at 12:35 PM, S Ahmed wrote: > > > I'm testing my controller, and confirming that a logged-in user can view > the page. > > > > I have a session_helper that does this: > > > > def signed_in? > > !current_user.nil? > > end > > > > And the current_user is set with: > > > > def user_from_remember_token > > remember_token = cookies[:remember_token] > > User.find_by_remember_token(remember_token) unless > remember_token.nil? > > end > > > > > > So in my controller, I need to fake a login somehow, how should I go > about doing this? > > > > I'm not sure if this is the best place for it, but so far what I tried > was creating a method in my spec_helper's config block: > > > > def test_sign_in(user) > > controller.sign_in(user) > > end > > > > (I get my user object from factorygirl). > > > > So my controller_spec looks like: > > > > > > describe "index" do > > before(:each) do > > @user = Factory(:user) > > test_sign_in(@user) > > end > > > > it "should be successful" do > > get 'index' > > controller.current_user.should == @user > > response.should be_success > > end > > end > > > > > > It is failing with current_user being nil. > > > > Should I just somehow stub the call to 'user_from_remember_token' to > return the factorygirl user? > > How can I do this? > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > Just set the :remember_token cookie: > > https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/cookies > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From myron.marston at gmail.com Sun Mar 4 07:37:24 2012 From: myron.marston at gmail.com (Myron Marston) Date: Sat, 3 Mar 2012 23:37:24 -0800 (PST) Subject: [rspec-users] Request for comments: additional new expectation syntax Message-ID: <22eb9dcb-098f-4cf3-a12f-1b22d20ca3d4@x7g2000pbi.googlegroups.com> I've opened a pull request with an initial implementation of using `expect` to set normal expectations in addition to block ones: expect(something).to be_awesome Note that it's not simply the syntax change that's driving the possibility of introducing this. It's the fact that the `should` and `should_not` syntax has occasionally been prone to problems that the `expect` syntax would avoid entirely. It'd be great to get feedback from other RSpec users about this. Here's the pull request: https://github.com/rspec/rspec-expectations/pull/119 Please leave comments there. I don't often have time to keep up with the rspec mailing list so I likely won't see comments here. From dchelimsky at gmail.com Sun Mar 4 08:55:39 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 4 Mar 2012 02:55:39 -0600 Subject: [rspec-users] what RSpec approach could I use for this. In-Reply-To: References: <6154714fb3078c63792ba09e24c7190f@ruby-forum.com> Message-ID: On Sat, Mar 3, 2012 at 4:29 PM, Justin Ko wrote: > > On Mar 3, 2012, at 2:55 PM, Greg C. wrote: > >> Background: ?So I have roughly: >> >> class A >> ? def calculate_input_datetimes >> ? ? ?# do stuff to calculate datetimes - then for each one identified >> ? ? ?process_datetimes(my_datetime_start, my_datetime_end) >> ? end >> >> ? def process_datetimes(input_datetime_start, input_datetime_end) >> ? ? ?# do stuff >> ? end >> end >> >> So: >> * I want to test that calculate_input_datetimes algorithms are working >> and calculating the correct datetimes to pass to process_datetimes >> * I know I can STUB out process_datetimes so that it's code won't be >> involved in the test >> >> QUESTION: ?How can I setup the rspec test however so I can specifically >> test that the correct datestimes were attempted to be passed over to >> process_datetimes, ?So for a given spec test that process_datetimes was >> called three (3) times say with the following parameters passed: >> >> * 2012-03-03T00:00:00+00:00, 2012-03-09T23:59:59+00:00 >> * 2012-03-10T00:00:00+00:00, 2012-03-16T23:59:59+00:00 >> * 2012-03-17T00:00:00+00:00, 2012-03-23T23:59:59+00:00 > Setup 3 individual expectations: > > a_instance.should_receive(:process_datetimes).with(input_datetime_start_1, datetime_end_1).once.ordered > a_instance.should_receive(:process_datetimes).with(input_datetime_start_2, datetime_end_2).once.ordered > > Your 2 methods should be tested in isolation (unless #process_datetimes is private). So for #calculate_input_datetimes, just ensure that #process_datetimes is called with the correct arguments. The, test #processs_datetimes in isolation. Stubbing internals of the subject of the example can hide subtle bugs. I'd just specify this in terms of initial state and output of calculate_input_datetimes. I'll be more specific if you will (i.e. what does this object/method actually do)? From sahmed1020 at gmail.com Sun Mar 4 13:40:57 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Sun, 4 Mar 2012 08:40:57 -0500 Subject: [rspec-users] testing sessions Message-ID: I want to test if my sessions logic works. Session: id user_id When I create a new session, if there was a previous session row in the db with user_id = xxx, it should delete it first, then create a new row. How could I test this scenerio? So far I have: require 'spec_helper' describe Session do let(:session) { FactoryGirl.create(:session) } subject { session } it { should be_valid } describe "a new session" do s1 = FactoryGirl.build(:session) s2 = FactoryGirl.build(:session) user = FactoryGirl.create(:user) s1.user_id = user.id s1.save! #should change(Session, :count).by(1) end end I can't seem to figure out how to use the "should change Session count by 1". -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Sun Mar 4 18:34:22 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 4 Mar 2012 12:34:22 -0600 Subject: [rspec-users] testing sessions In-Reply-To: References: Message-ID: On Sun, Mar 4, 2012 at 7:40 AM, S Ahmed wrote: > I want to test if my sessions logic works. > > Session: > ?id > ?user_id > > When I create a new session, if there was a previous session row in the db > with user_id = xxx, it should delete it first, then create a new row. > > How could I test this scenerio? > > So far I have: > > require 'spec_helper' > > describe Session do > ? let(:session) { FactoryGirl.create(:session) } > ? subject { session } > ? it { should be_valid } > > ? describe "a new session" do > ? ? s1 = FactoryGirl.build(:session) > ? ? s2 = FactoryGirl.build(:session) > ? ? user = FactoryGirl.create(:user) > > ? ? s1.user_id = user.id > ? ? s1.save! > ? ? #should change(Session, :count).by(1) > ? end > end > > I can't seem to figure out how to use the "should change Session count by > 1". First - read the docs at http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:change to learn how to use the `change` matcher properly. Also look at http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:expect so you'll understand my suggestion below. Second - you say above "When I create a new session, if there was a previous session row in the db with user_id = xxx, it should delete it first, then create a new row." This suggests that you want Session.count _not_ to change at all: user = FactoryGirl(:user) session = FactoryGirl(:session, :user_id => user.id) expect { session.save! }.not_to change(Session, :count) HTH, David From sahmed1020 at gmail.com Sun Mar 4 21:46:28 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Sun, 4 Mar 2012 16:46:28 -0500 Subject: [rspec-users] testing sessions In-Reply-To: References: Message-ID: great thanks. should a var created in a block be reachable outside the block? it "...." do expect { session = Session....... }.not_to change(Session, :count) session.guid.should xxxx end I tried that but was a bit confused, whatever is in the expect block is isolated right? On Sun, Mar 4, 2012 at 1:34 PM, David Chelimsky wrote: > On Sun, Mar 4, 2012 at 7:40 AM, S Ahmed wrote: > > I want to test if my sessions logic works. > > > > Session: > > id > > user_id > > > > When I create a new session, if there was a previous session row in the > db > > with user_id = xxx, it should delete it first, then create a new row. > > > > How could I test this scenerio? > > > > So far I have: > > > > require 'spec_helper' > > > > describe Session do > > let(:session) { FactoryGirl.create(:session) } > > subject { session } > > it { should be_valid } > > > > describe "a new session" do > > s1 = FactoryGirl.build(:session) > > s2 = FactoryGirl.build(:session) > > user = FactoryGirl.create(:user) > > > > s1.user_id = user.id > > s1.save! > > #should change(Session, :count).by(1) > > end > > end > > > > I can't seem to figure out how to use the "should change Session count by > > 1". > > First - read the docs at > http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:change to > learn how to use the `change` matcher properly. Also look at > http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:expect so > you'll understand my suggestion below. > > Second - you say above "When I create a new session, if there was a > previous session row in the db with user_id = xxx, it should delete it > first, then create a new row." This suggests that you want > Session.count _not_ to change at all: > > user = FactoryGirl(:user) > session = FactoryGirl(:session, :user_id => user.id) > expect { session.save! }.not_to change(Session, :count) > > HTH, > David > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zach.dennis at gmail.com Sun Mar 4 22:15:43 2012 From: zach.dennis at gmail.com (Zach Dennis) Date: Sun, 4 Mar 2012 17:15:43 -0500 Subject: [rspec-users] testing sessions In-Reply-To: References: Message-ID: On Sun, Mar 4, 2012 at 4:46 PM, S Ahmed wrote: > great thanks. > > should a var created in a block be reachable outside the block? Nope, not unless you defined the variable outside of the block to start with. > > it "...." do > ? expect { session = Session....... ? }.not_to change(Session, :count) > ? session.guid.should xxxx > end > > I tried that but was a bit confused, whatever is in the expect block is > isolated right? Correct. Currently your session variable is only scoped to the block that is passed to "expect". Anything outside of that block doesn't know anything about the session variable. Zach > > > On Sun, Mar 4, 2012 at 1:34 PM, David Chelimsky > wrote: >> >> On Sun, Mar 4, 2012 at 7:40 AM, S Ahmed wrote: >> > I want to test if my sessions logic works. >> > >> > Session: >> > ?id >> > ?user_id >> > >> > When I create a new session, if there was a previous session row in the >> > db >> > with user_id = xxx, it should delete it first, then create a new row. >> > >> > How could I test this scenerio? >> > >> > So far I have: >> > >> > require 'spec_helper' >> > >> > describe Session do >> > ? let(:session) { FactoryGirl.create(:session) } >> > ? subject { session } >> > ? it { should be_valid } >> > >> > ? describe "a new session" do >> > ? ? s1 = FactoryGirl.build(:session) >> > ? ? s2 = FactoryGirl.build(:session) >> > ? ? user = FactoryGirl.create(:user) >> > >> > ? ? s1.user_id = user.id >> > ? ? s1.save! >> > ? ? #should change(Session, :count).by(1) >> > ? end >> > end >> > >> > I can't seem to figure out how to use the "should change Session count >> > by >> > 1". >> >> First - read the docs at >> http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:change to >> learn how to use the `change` matcher properly. Also look at >> http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:expect so >> you'll understand my suggestion below. >> >> Second - you say above "When I create a new session, if there was a >> previous session row in the db with user_id = xxx, it should delete it >> first, then create a new row." This suggests that you want >> Session.count _not_ to change at all: >> >> user = FactoryGirl(:user) >> session = FactoryGirl(:session, :user_id => user.id) >> expect { session.save! }.not_to change(Session, :count) >> >> HTH, >> David >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -- -- @zachdennis http://www.continuousthinking.com http://www.mutuallyhuman.com From apremdas at gmail.com Mon Mar 5 12:05:28 2012 From: apremdas at gmail.com (Andrew Premdas) Date: Mon, 5 Mar 2012 12:05:28 +0000 Subject: [rspec-users] what RSpec approach could I use for this. In-Reply-To: <6154714fb3078c63792ba09e24c7190f@ruby-forum.com> References: <6154714fb3078c63792ba09e24c7190f@ruby-forum.com> Message-ID: On 3 March 2012 21:55, Greg C. wrote: > Background: So I have roughly: > > class A > def calculate_input_datetimes > # do stuff to calculate datetimes - then for each one identified > process_datetimes(my_datetime_start, my_datetime_end) > end > > My thoughts on this is that this method is fundamentally flawed as it is working at two different levels of abstraction. The first is doing date calculation using algorithms and the second is delegating responsibility by calling processing dates. Instead you should have a method that just delegates e.g. def foo calculate_datetimes process_datetimes end Now your test for foo is all about it calling calculate... and then calling process... with the results. (I'll leave it up to you to determine how you pass the results. The actual methods you delegate too now can be tested independently. Your current tests are actually telling you this, because they want to do one thing and then another and that is making them harder to implement HTH Andrew > def process_datetimes(input_datetime_start, input_datetime_end) > # do stuff > end > end > > So: > * I want to test that calculate_input_datetimes algorithms are working > and calculating the correct datetimes to pass to process_datetimes > * I know I can STUB out process_datetimes so that it's code won't be > involved in the test > > QUESTION: How can I setup the rspec test however so I can specifically > test that the correct datestimes were attempted to be passed over to > process_datetimes, So for a given spec test that process_datetimes was > called three (3) times say with the following parameters passed: > > * 2012-03-03T00:00:00+00:00, 2012-03-09T23:59:59+00:00 > * 2012-03-10T00:00:00+00:00, 2012-03-16T23:59:59+00:00 > * 2012-03-17T00:00:00+00:00, 2012-03-23T23:59:59+00:00 > > > thanks > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- ------------------------ Andrew Premdas blog.andrew.premdas.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.grosmann at gmail.com Mon Mar 5 08:18:18 2012 From: r.grosmann at gmail.com (ruud144) Date: Mon, 5 Mar 2012 00:18:18 -0800 (PST) Subject: [rspec-users] syntax for custom messages Message-ID: hi group, I read that expectations can print a custom message on failure using a syntax like cars.should be_empty, "Cars left" But when I try this syntax for this expectation: string.should == 'Cars left', 'Yippee, no cars anymore' I get a syntax error: syntax error, unexpected ',', expecting keyword_end (SyntaxError) I want two things: - I want a syntax error free expectation for should == - I want to understand what the mechanism is. I am afraid that my ruby knowledge is not sufficient. Clearly, should is a function, but what are be_empty and == then? Parameters? Can I use parentheses? Thanks for helping me! Ruud From dchelimsky at gmail.com Mon Mar 5 14:10:01 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 5 Mar 2012 08:10:01 -0600 Subject: [rspec-users] syntax for custom messages In-Reply-To: References: Message-ID: On Mon, Mar 5, 2012 at 2:18 AM, ruud144 wrote: > hi group, > > I read that expectations can print a custom message on failure using a > syntax like > > cars.should be_empty, "Cars left" > > But when I try this syntax for this expectation: > > string.should == 'Cars left', 'Yippee, no cars anymore' > > I get a syntax error: > > syntax error, unexpected ',', expecting keyword_end (SyntaxError) > > I want two things: > - I want a syntax error free expectation for should == Can't have it because Ruby won't parse it. The reason `a.should == b` works is because Ruby parses that as `a.should.==(b)`. There is an `eq` matcher, which is the recommended approach these days for this (and other similar) reason(s): string.should eq('Cars left'), 'Yippee, no cars anymore' > - I want to understand what the mechanism is. I am afraid that my ruby > knowledge is not sufficient. Clearly, should is a function, but what > are be_empty and == then? Parameters? Can I use parentheses? Here are the bits of code relevant to your question: https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/expectations/extensions/kernel.rb#L11-13 https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/expectations/handler.rb#L4-7 The `should` method delegates to `handle_matcher`, passing along the matcher (which might be nil), optional message (also might be nil), and optional block (again, might be nil). When you use `a.should == b`, the matcher itself is nil, and the `handle_matcher` method delegates to a `PositiveOperatorMatcher` (there is also a `NegativeOperatorMatcher` for `should_not`), otherwise it handles the matcher itself. Parens won't help you here because everything after `==` is bound to `==`, not should - there's no way (that I know of) to bind the message to `should`. It's feasible to pass an array to `==`: string.should == ['Cars left', 'Yippee, no cars anymore'] ... but then we'd be comparing `string` with the list, so that wouldn't work either (nor would it make any sense). Your best bet is using the `eq` matcher, as described above. HTH, David > > Thanks for helping me! > > Ruud From jko170 at gmail.com Mon Mar 5 14:47:06 2012 From: jko170 at gmail.com (Justin Ko) Date: Mon, 5 Mar 2012 07:47:06 -0700 Subject: [rspec-users] what RSpec approach could I use for this. In-Reply-To: References: <6154714fb3078c63792ba09e24c7190f@ruby-forum.com> Message-ID: On Mar 5, 2012, at 5:05 AM, Andrew Premdas wrote: > > On 3 March 2012 21:55, Greg C. wrote: > Background: So I have roughly: > > class A > def calculate_input_datetimes > # do stuff to calculate datetimes - then for each one identified > process_datetimes(my_datetime_start, my_datetime_end) > end > > My thoughts on this is that this method is fundamentally flawed as it is working at two different levels of abstraction. The first is doing date calculation using algorithms and the second is delegating responsibility by calling processing dates. Instead you should have a method that just delegates e.g. > > def foo > calculate_datetimes > process_datetimes > end > > Now your test for foo is all about it calling calculate... and then calling process... with the results. (I'll leave it up to you to determine how you pass the results. > > The actual methods you delegate too now can be tested independently. > > Your current tests are actually telling you this, because they want to do one thing and then another and that is making them harder to implement This. Your tests tell you so much about the implementation code. Hard to test? Bad design. > > HTH > > Andrew > > def process_datetimes(input_datetime_start, input_datetime_end) > # do stuff > end > end > > So: > * I want to test that calculate_input_datetimes algorithms are working > and calculating the correct datetimes to pass to process_datetimes > * I know I can STUB out process_datetimes so that it's code won't be > involved in the test > > QUESTION: How can I setup the rspec test however so I can specifically > test that the correct datestimes were attempted to be passed over to > process_datetimes, So for a given spec test that process_datetimes was > called three (3) times say with the following parameters passed: > > * 2012-03-03T00:00:00+00:00, 2012-03-09T23:59:59+00:00 > * 2012-03-10T00:00:00+00:00, 2012-03-16T23:59:59+00:00 > * 2012-03-17T00:00:00+00:00, 2012-03-23T23:59:59+00:00 > > > thanks > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > > > -- > ------------------------ > Andrew Premdas > blog.andrew.premdas.org > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Mon Mar 5 15:13:57 2012 From: jko170 at gmail.com (Justin Ko) Date: Mon, 5 Mar 2012 08:13:57 -0700 Subject: [rspec-users] syntax for custom messages In-Reply-To: References: Message-ID: On Mar 5, 2012, at 1:18 AM, ruud144 wrote: > hi group, > > I read that expectations can print a custom message on failure using a > syntax like > > cars.should be_empty, "Cars left" This is because RSpec predicate matchers can accept a block: https://github.com/rspec/rspec-expectations/blob/master/spec/rspec/matchers/be_spec.rb#L174 > > But when I try this syntax for this expectation: > > string.should == 'Cars left', 'Yippee, no cars anymore' This will work: string.should eq('Cars left'), 'Yippee, no cars anymore' > > I get a syntax error: > > syntax error, unexpected ',', expecting keyword_end (SyntaxError) > > I want two things: > - I want a syntax error free expectation for should == > - I want to understand what the mechanism is. I am afraid that my ruby > knowledge is not sufficient. Clearly, should is a function, but what > are be_empty and == then? Parameters? Can I use parentheses? > > Thanks for helping me! > > Ruud > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Personally, I think custom messages should be deprecated. Nobody uses them and it's impossible to support them 100% of the time (as in predicate matchers). The default failure messages work 99% of the time, and if it doesn't, a custom matcher is the way to go. David? From r.grosmann at gmail.com Mon Mar 5 16:28:19 2012 From: r.grosmann at gmail.com (ruud144) Date: Mon, 5 Mar 2012 08:28:19 -0800 (PST) Subject: [rspec-users] syntax for custom messages In-Reply-To: References: Message-ID: <36e4b8e9-74b3-4b4f-9de0-6a2d44e904ba@q12g2000vbt.googlegroups.com> Thank you for your explanation! Ruud On Mar 5, 3:10?pm, David Chelimsky wrote: > On Mon, Mar 5, 2012 at 2:18 AM, ruud144 wrote: > > hi group, > > > I read that expectations can print a custom message on failure using a > > syntax like > > > cars.should be_empty, "Cars left" > > > But when I try this syntax for this expectation: > > > string.should == 'Cars left', 'Yippee, no cars anymore' > > > I get a syntax error: > > > syntax error, unexpected ',', expecting keyword_end (SyntaxError) > > > I want two things: > > - I want a syntax error free expectation for should == > > Can't have it because Ruby won't parse it. The reason `a.should == b` > works is because Ruby parses that as `a.should.==(b)`. > > There is an `eq` matcher, which is the recommended approach these days > for this (and other similar) reason(s): > > string.should eq('Cars left'), 'Yippee, no cars anymore' > > > - I want to understand what the mechanism is. I am afraid that my ruby > > knowledge is not sufficient. Clearly, should is a function, but what > > are be_empty and == then? Parameters? Can I use parentheses? > > Here are the bits of code relevant to your question: > > https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/exp... > > https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/exp... > > The `should` method delegates to `handle_matcher`, passing along the > matcher (which might be nil), optional message (also might be nil), > and optional block (again, might be nil). > > When you use `a.should == b`, the matcher itself is nil, and the > `handle_matcher` method delegates to a `PositiveOperatorMatcher` > (there is also a `NegativeOperatorMatcher` for `should_not`), > otherwise it handles the matcher itself. > > Parens won't help you here because everything after `==` is bound to > `==`, not should - there's no way (that I know of) to bind the message > to `should`. It's feasible to pass an array to `==`: > > ? string.should == ['Cars left', 'Yippee, no cars anymore'] > > ... but then we'd be comparing `string` with the list, so that > wouldn't work either (nor would it make any sense). > > Your best bet is using the `eq` matcher, as described above. > > HTH, > David > > > > > Thanks for helping me! > > > Ruud > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From matt at mattwynne.net Wed Mar 7 07:22:39 2012 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 7 Mar 2012 07:22:39 +0000 Subject: [rspec-users] Asserting on a yield Message-ID: Hi all, I'm spec'ing a method that yields a value. Right now, I spec it like this: result = nil thing.do_stuff { |value| result = value } result.should == expected This feels like too much ceremony. What I want to do is something more this: thing.do_stuff.should yield_value(expected) Is there anything built into RSpec to let me do this? If not, how do other people test yields? cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From mortenmoellerriis at gmail.com Wed Mar 7 11:39:13 2012 From: mortenmoellerriis at gmail.com (=?iso-8859-1?Q?Morten_M=F8ller_Riis?=) Date: Wed, 7 Mar 2012 12:39:13 +0100 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: Message-ID: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> How about this? thing.do_stuff(&:to_s).should == expected Best regards Morten M?ller Riis On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote: > Hi all, > > I'm spec'ing a method that yields a value. Right now, I spec it like this: > > result = nil > thing.do_stuff { |value| result = value } > result.should == expected > > This feels like too much ceremony. What I want to do is something more this: > > thing.do_stuff.should yield_value(expected) > > Is there anything built into RSpec to let me do this? If not, how do other people test yields? > > cheers, > Matt > > -- > Freelance programmer & coach > Author, http://pragprog.com/book/hwcuc/the-cucumber-book > Founder, http://www.relishapp.com/ > Twitter, https://twitter.com/mattwynne > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -------------- next part -------------- An HTML attachment was scrubbed... URL: From ken.chien at gmail.com Wed Mar 7 15:12:43 2012 From: ken.chien at gmail.com (Ken Chien) Date: Wed, 7 Mar 2012 10:12:43 -0500 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: Message-ID: Hi Matt, On Wed, Mar 7, 2012 at 2:22 AM, Matt Wynne wrote: > Hi all, > > I'm spec'ing a method that yields a value. Right now, I spec it like this: > > result = nil > thing.do_stuff { |value| result = value } > result.should == expected > > This feels like too much ceremony. What I want to do is something more > this: > > thing.do_stuff.should yield_value(expected) > > Is there anything built into RSpec to let me do this? If not, how do other > people test yields? > > I came across this about a month or two ago. You can do this: thing.should_receive(:do_stuff).and_yield( whatever) See http://rubydoc.info/gems/rspec-mocks/frames for more details. Regards, Ken -------------- next part -------------- An HTML attachment was scrubbed... URL: From matt at mattwynne.net Wed Mar 7 17:53:41 2012 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 7 Mar 2012 17:53:41 +0000 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: Message-ID: <0910DED8-28D9-4D18-BBB4-836730954C17@mattwynne.net> On 7 Mar 2012, at 15:12, Ken Chien wrote: > Hi Matt, > On Wed, Mar 7, 2012 at 2:22 AM, Matt Wynne wrote: > Hi all, > > I'm spec'ing a method that yields a value. Right now, I spec it like this: > > result = nil > thing.do_stuff { |value| result = value } > result.should == expected > > This feels like too much ceremony. What I want to do is something more this: > > thing.do_stuff.should yield_value(expected) > > Is there anything built into RSpec to let me do this? If not, how do other people test yields? > > > I came across this about a month or two ago. > You can do this: > thing.should_receive(:do_stuff).and_yield( whatever) Surely that sets up a mocked collaborator to respond by yielding something, doesn't it? That's not what I'm looking for. I want to specify that a method on the class I'm testing yields a certain value. cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From matt at mattwynne.net Wed Mar 7 17:56:30 2012 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 7 Mar 2012 17:56:30 +0000 Subject: [rspec-users] Asserting on a yield In-Reply-To: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> Message-ID: On 7 Mar 2012, at 11:39, Morten M?ller Riis wrote: > On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote: > >> Hi all, >> >> I'm spec'ing a method that yields a value. Right now, I spec it like this: >> >> result = nil >> thing.do_stuff { |value| result = value } >> result.should == expected >> >> This feels like too much ceremony. What I want to do is something more this: >> >> thing.do_stuff.should yield_value(expected) >> >> Is there anything built into RSpec to let me do this? If not, how do other people test yields? >> >> cheers, >> Matt > How about this? > > thing.do_stuff(&:to_s).should == expected Yes, that's a neat hack, but I'd prefer to be able to assert on the actual yielded value, instead of the result of calling an arbitrary method on it. Doesn't anyone else do this? cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Wed Mar 7 18:16:17 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 7 Mar 2012 12:16:17 -0600 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> Message-ID: On Wed, Mar 7, 2012 at 11:56 AM, Matt Wynne wrote: > > On 7 Mar 2012, at 11:39, Morten M?ller Riis wrote: > > On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote: > > > Hi all, > > I'm spec'ing a method that yields a value. Right now, I spec it like this: > > ? ? result = nil > ? ? thing.do_stuff { |value| result = value } > ? ? result.should == expected > > This feels like too much ceremony. What I want to do is something more this: > > ? ?thing.do_stuff.should yield_value(expected) > > Is there anything built into RSpec to let me do this? If not, how do other > people test yields? > > cheers, > Matt > > > How about this? > > thing.do_stuff(&:to_s).should == expected > > > Yes, that's a neat hack, but I'd prefer to be able to assert on the actual > yielded value, instead of the result of calling an arbitrary method on it. thing.do_stuff(&:self) would be a bit less arbitrary :) > > Doesn't anyone else do this? > > cheers, > Matt > > -- > Freelance programmer &?coach > Author,?http://pragprog.com/book/hwcuc/the-cucumber-book > Founder,?http://www.relishapp.com/ > Twitter,?https://twitter.com/mattwynne > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From zach.dennis at gmail.com Wed Mar 7 18:26:40 2012 From: zach.dennis at gmail.com (Zach Dennis) Date: Wed, 7 Mar 2012 13:26:40 -0500 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> Message-ID: Matt, I have typically done what you are already doing, but I am also interested in the answer you seek. Another idea might be something like since #do-stuff would need to be invoked with a block: expect(thing, :do_stuff).to_yield val Zach On Wed, Mar 7, 2012 at 12:56 PM, Matt Wynne wrote: > > On 7 Mar 2012, at 11:39, Morten M?ller Riis wrote: > > On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote: > > > Hi all, > > I'm spec'ing a method that yields a value. Right now, I spec it like this: > > ? ? result = nil > ? ? thing.do_stuff { |value| result = value } > ? ? result.should == expected > > This feels like too much ceremony. What I want to do is something more this: > > ? ?thing.do_stuff.should yield_value(expected) > > Is there anything built into RSpec to let me do this? If not, how do other > people test yields? > > cheers, > Matt > > > How about this? > > thing.do_stuff(&:to_s).should == expected > > > Yes, that's a neat hack, but I'd prefer to be able to assert on the actual > yielded value, instead of the result of calling an arbitrary method on it. > > Doesn't anyone else do this? > > cheers, > Matt > > -- > Freelance programmer &?coach > Author,?http://pragprog.com/book/hwcuc/the-cucumber-book > Founder,?http://www.relishapp.com/ > Twitter,?https://twitter.com/mattwynne > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -- -- @zachdennis http://www.continuousthinking.com http://www.mutuallyhuman.com From dchelimsky at gmail.com Wed Mar 7 18:28:37 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 7 Mar 2012 12:28:37 -0600 Subject: [rspec-users] Asserting on a yield In-Reply-To: <0910DED8-28D9-4D18-BBB4-836730954C17@mattwynne.net> References: <0910DED8-28D9-4D18-BBB4-836730954C17@mattwynne.net> Message-ID: On Wed, Mar 7, 2012 at 11:53 AM, Matt Wynne wrote: > > On 7 Mar 2012, at 15:12, Ken Chien wrote: > > Hi Matt, > On Wed, Mar 7, 2012 at 2:22 AM, Matt Wynne wrote: >> >> Hi all, >> >> I'm spec'ing a method that yields a value. Right now, I spec it like this: >> >> ? ? result = nil >> ? ? thing.do_stuff { |value| result = value } >> ? ? result.should == expected >> >> This feels like too much ceremony. What I want to do is something more >> this: >> >> ? ?thing.do_stuff.should yield_value(expected) >> >> Is there anything built into RSpec to let me do this? If not, how do other >> people test yields? >> > > I came across this about a month or two ago. > You can do this: > ??? thing.should_receive(:do_stuff).and_yield( whatever) > > > Surely that sets up a mocked collaborator to respond by yielding something, doesn't it? Right. > That's not what I'm looking for. Right again. > I want to specify that a method on the class I'm testing yields a certain value. > > cheers, > Matt > > -- > Freelance programmer &?coach > Author,?http://pragprog.com/book/hwcuc/the-cucumber-book > Founder,?http://www.relishapp.com/ > Twitter,?https://twitter.com/mattwynne > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From matt at mattwynne.net Wed Mar 7 20:30:23 2012 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 7 Mar 2012 20:30:23 +0000 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> Message-ID: On 7 Mar 2012, at 18:26, Zach Dennis wrote: > Matt, > > I have typically done what you are already doing, but I am also > interested in the answer you seek. Another idea might be something > like since #do-stuff would need to be invoked with a block: > > expect(thing, :do_stuff).to_yield val Yeah, I like that. Is that real yet or is this just an idea? > > Zach > > On Wed, Mar 7, 2012 at 12:56 PM, Matt Wynne wrote: >> >> On 7 Mar 2012, at 11:39, Morten M?ller Riis wrote: >> >> On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote: >> >> >> Hi all, >> >> I'm spec'ing a method that yields a value. Right now, I spec it like this: >> >> result = nil >> thing.do_stuff { |value| result = value } >> result.should == expected >> >> This feels like too much ceremony. What I want to do is something more this: >> >> thing.do_stuff.should yield_value(expected) >> >> Is there anything built into RSpec to let me do this? If not, how do other >> people test yields? >> >> cheers, >> Matt >> >> >> How about this? >> >> thing.do_stuff(&:to_s).should == expected >> >> >> Yes, that's a neat hack, but I'd prefer to be able to assert on the actual >> yielded value, instead of the result of calling an arbitrary method on it. >> >> Doesn't anyone else do this? >> >> cheers, >> Matt >> >> -- >> Freelance programmer & coach >> Author, http://pragprog.com/book/hwcuc/the-cucumber-book >> Founder, http://www.relishapp.com/ >> Twitter, https://twitter.com/mattwynne >> >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > -- > > -- > @zachdennis > http://www.continuousthinking.com > http://www.mutuallyhuman.com > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From matt at mattwynne.net Wed Mar 7 20:32:19 2012 From: matt at mattwynne.net (Matt Wynne) Date: Wed, 7 Mar 2012 20:32:19 +0000 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> Message-ID: On 7 Mar 2012, at 18:16, David Chelimsky wrote: > On Wed, Mar 7, 2012 at 11:56 AM, Matt Wynne wrote: >> >> On 7 Mar 2012, at 11:39, Morten M?ller Riis wrote: >> >> On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote: >> >> >> Hi all, >> >> I'm spec'ing a method that yields a value. Right now, I spec it like this: >> >> result = nil >> thing.do_stuff { |value| result = value } >> result.should == expected >> >> This feels like too much ceremony. What I want to do is something more this: >> >> thing.do_stuff.should yield_value(expected) >> >> Is there anything built into RSpec to let me do this? If not, how do other >> people test yields? >> >> cheers, >> Matt >> >> >> How about this? >> >> thing.do_stuff(&:to_s).should == expected >> >> >> Yes, that's a neat hack, but I'd prefer to be able to assert on the actual >> yielded value, instead of the result of calling an arbitrary method on it. > > thing.do_stuff(&:self) would be a bit less arbitrary :) It would, but does it work? I assumed it would too but here on my Ruby 1.9.2 it gives me a NoMethodError :( cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Wed Mar 7 20:42:01 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 7 Mar 2012 14:42:01 -0600 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> Message-ID: On Wed, Mar 7, 2012 at 2:32 PM, Matt Wynne wrote: > > On 7 Mar 2012, at 18:16, David Chelimsky wrote: > > On Wed, Mar 7, 2012 at 11:56 AM, Matt Wynne wrote: > > > On 7 Mar 2012, at 11:39, Morten M?ller Riis wrote: > > > On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote: > > > > Hi all, > > > I'm spec'ing a method that yields a value. Right now, I spec it like this: > > > ? ? result = nil > > ? ? thing.do_stuff { |value| result = value } > > ? ? result.should == expected > > > This feels like too much ceremony. What I want to do is something more this: > > > ? ?thing.do_stuff.should yield_value(expected) > > > Is there anything built into RSpec to let me do this? If not, how do other > > people test yields? > > > cheers, > > Matt > > > > How about this? > > > thing.do_stuff(&:to_s).should == expected > > > > Yes, that's a neat hack, but I'd prefer to be able to assert on the actual > > yielded value, instead of the result of calling an arbitrary method on it. > > > thing.do_stuff(&:self) would be a bit less arbitrary :) > > > It would, but does it work? > > I assumed it would too but here on my Ruby 1.9.2 it gives me a?NoMethodError :( I had assumed as well, and you know what they say about ASSuming! From jko170 at gmail.com Thu Mar 8 05:59:46 2012 From: jko170 at gmail.com (Justin Ko) Date: Wed, 7 Mar 2012 22:59:46 -0700 Subject: [rspec-users] color option In-Reply-To: <4CC55BC30D9D44F581A56BFCC6A00ECB@gmail.com> References: <4CC55BC30D9D44F581A56BFCC6A00ECB@gmail.com> Message-ID: <6A5BA505-B91D-409B-8C60-CE0E4799A085@gmail.com> On Feb 27, 2012, at 10:58 AM, jed schneider wrote: > we are on rspec 2.6.4 and for awhile my color flag seems to be ignored when running specs in my .rspec file but my formatting option is picking up. Did the --color flag get changed? > > my .rspec file > --color > -f d > > thanks! > > -- > jed schneider > Sent with Sparrow > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Works for me on 2.8. Do you maybe have this in your RSpec.configuration: RSpec.configure do |config| config.color = false end ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From mortenmoellerriis at gmail.com Thu Mar 8 11:57:05 2012 From: mortenmoellerriis at gmail.com (=?iso-8859-1?Q?Morten_M=F8ller_Riis?=) Date: Thu, 8 Mar 2012 12:57:05 +0100 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> Message-ID: <887CD80B-70C3-4BF6-801E-B890F911D777@gmail.com> &:self won't work because self isn't an instance method on the object, eg. some_object.self won't work either. If you wan't something cleaner couldn't you use this method in spec_helper.rb ? def block_yield(object, method) object.send(method) { yield if block_given? } end Then: block_yield(thing, :do_stuff).should == "value" Mvh Morten M?ller Riis On Mar 7, 2012, at 9:42 PM, David Chelimsky wrote: > On Wed, Mar 7, 2012 at 2:32 PM, Matt Wynne wrote: >> >> On 7 Mar 2012, at 18:16, David Chelimsky wrote: >> >> On Wed, Mar 7, 2012 at 11:56 AM, Matt Wynne wrote: >> >> >> On 7 Mar 2012, at 11:39, Morten M?ller Riis wrote: >> >> >> On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote: >> >> >> >> Hi all, >> >> >> I'm spec'ing a method that yields a value. Right now, I spec it like this: >> >> >> result = nil >> >> thing.do_stuff { |value| result = value } >> >> result.should == expected >> >> >> This feels like too much ceremony. What I want to do is something more this: >> >> >> thing.do_stuff.should yield_value(expected) >> >> >> Is there anything built into RSpec to let me do this? If not, how do other >> >> people test yields? >> >> >> cheers, >> >> Matt >> >> >> >> How about this? >> >> >> thing.do_stuff(&:to_s).should == expected >> >> >> >> Yes, that's a neat hack, but I'd prefer to be able to assert on the actual >> >> yielded value, instead of the result of calling an arbitrary method on it. >> >> >> thing.do_stuff(&:self) would be a bit less arbitrary :) >> >> >> It would, but does it work? >> >> I assumed it would too but here on my Ruby 1.9.2 it gives me a NoMethodError :( > > I had assumed as well, and you know what they say about ASSuming! > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From sahmed1020 at gmail.com Thu Mar 8 23:15:00 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Thu, 8 Mar 2012 18:15:00 -0500 Subject: [rspec-users] how to refactor signin process for re-use? Message-ID: In my authenticate_pages.spec (requests) I do the following to test if the signin worked: describe "with valid information" do #let(:account) { FactoryGirl.create(:account) } let(:user) { FactoryGirl.create(:user) } before do fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign in" end it { should have_link('Sign out', href: signout_path) } it { should_not have_link('Sign in', href: signin_path) } end Now in my other controllers that assume the user is signed in, how can I refactor this and put it somewhere that I can just call to make the user signed in so I can test pages that assume the user is already signed in? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Fri Mar 9 03:38:31 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 8 Mar 2012 21:38:31 -0600 Subject: [rspec-users] how to refactor signin process for re-use? In-Reply-To: References: Message-ID: On Thu, Mar 8, 2012 at 5:15 PM, S Ahmed wrote: > In my authenticate_pages.spec (requests) I do the following to test if the > signin worked: > > ?describe "with valid information" do > ? ? ? #let(:account) { FactoryGirl.create(:account) } > ? ? ? let(:user) { FactoryGirl.create(:user) } > > ? ? ? before do > ? ? ? ? fill_in "Email", with: user.email > ? ? ? ? fill_in "Password", with: user.password > ? ? ? ? click_button "Sign in" > ? ? ? end > > ? ? ? it { should have_link('Sign out', href: signout_path) } > ? ? ? it { should_not have_link('Sign in', href: signin_path) } > ? ? end > > > Now in my other controllers that assume the user is signed in, how can I > refactor this and put it somewhere that I can just call to make the user > signed in so I can test pages that assume the user is already signed in? Here's one pattern I've seen (and used): def login_as(user) fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign in" end describe "things" do before { sign_in_as(FactoryGirl.create:(user) } describe "GET /thing" do # ... end end HTH, David From lists at ruby-forum.com Fri Mar 9 13:06:16 2012 From: lists at ruby-forum.com (U. M.) Date: Fri, 09 Mar 2012 14:06:16 +0100 Subject: [rspec-users] Sufficiently tested? Message-ID: Hi everybody, I have a general question about how many different cases have to be covered in one Example. Considering the famous Fizzbuzz program, you should test if your output is either "Fizz", "buzz", "Fizzbuzz" or just the number (between 1 and 100). Pretty simple. However, I often see solutions of testing multiples of 3 only by looking at some relevant values like 6, 9 etc. it "should return 'fizz' when number is divisible by 3" do @fizzbuzz.calculate(3).should == 'fizz' @fizzbuzz.calculate(6).should == 'fizz' @fizzbuzz.calculate(9).should == 'fizz' end So far, so good. But what if I claim that the test will fail in case of 12? It's actually not covered and we are just assuming, that all further multiples will work as well, because these do. My idea was to check all multiples right away in a loop. it "should return Fizz if number is divisible only by 3" do (1..100).each do |number| @fizzbuzz.calculate(number).should == "Fizz" if number % 3 == 0 && number % 5 != 0 end end Given that the calculate method works fine, both ways will pass. But my question is: Does my solution more than it actually needs to or is the first one in fact a little too lazy? Thanks for your thoughts. Ulfmann -- Posted via http://www.ruby-forum.com/. From dchelimsky at gmail.com Fri Mar 9 14:15:59 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 9 Mar 2012 08:15:59 -0600 Subject: [rspec-users] Sufficiently tested? In-Reply-To: References: Message-ID: On Fri, Mar 9, 2012 at 7:06 AM, U. M. wrote: > Hi everybody, > > I have a general question about how many different cases have to be > covered in one Example. > > Considering the famous Fizzbuzz program, you should test if your output > is either "Fizz", "buzz", "Fizzbuzz" or just the number (between 1 and > 100). Pretty simple. However, I often see solutions of testing multiples > of 3 only by looking at some relevant values like 6, 9 etc. > > it "should return 'fizz' when number is divisible by 3" do > ? ?@fizzbuzz.calculate(3).should == 'fizz' > ? ?@fizzbuzz.calculate(6).should == 'fizz' > ? ?@fizzbuzz.calculate(9).should == 'fizz' > end > > So far, so good. But what if I claim that the test will fail in case of > 12? It's actually not covered and we are just assuming, that all further > multiples will work as well, because these do. My idea was to check all > multiples right away in a loop. > > it "should return Fizz if number is divisible only by 3" do > ?(1..100).each do |number| > ? ?@fizzbuzz.calculate(number).should == "Fizz" if number % 3 == 0 && > ? ?number % 5 != 0 > ?end > end > > Given that the calculate method works fine, both ways will pass. But my > question is: Does my solution more than it actually needs to or is the > first one in fact a little too lazy? > > Thanks for your thoughts. > Ulfmann TDD (and testing, in general) is, at its core, a risk-reduction technique. Because every example is code that you have to maintain, there is a tradeoff between completeness and maintainability. The first example is very simple and easy to understand at first glance. The second example is less so, which increases the long term cost of maintainability, thereby increasing risk (by increasing cost). Additionally, TDD is a gray-box sport: it's not white-box because you're operating at the surface of the object under test; it's not black-box because you can see the implementation! For me, the questions are: * given the relative complexity of the problem and the implementation, do the examples provide me sufficient confidence that: * the implementation is sound? * the examples are likely to catch regressions? As soon as the answer to both of those questions is "yes", I move on. There is, of course, a ton of context that needs to be considered. Are you working on a team of developers that you trust to not to ridiculous shit? If so, then you don't need to protect against idiocy. If not, you might need more examples (and you might consider finding a new job). HTH, David From zach.dennis at gmail.com Fri Mar 9 14:27:03 2012 From: zach.dennis at gmail.com (Zach Dennis) Date: Fri, 9 Mar 2012 09:27:03 -0500 Subject: [rspec-users] how to refactor signin process for re-use? In-Reply-To: References: Message-ID: On Thu, Mar 8, 2012 at 10:38 PM, David Chelimsky wrote: > On Thu, Mar 8, 2012 at 5:15 PM, S Ahmed wrote: >> In my authenticate_pages.spec (requests) I do the following to test if the >> signin worked: >> >> ?describe "with valid information" do >> ? ? ? #let(:account) { FactoryGirl.create(:account) } >> ? ? ? let(:user) { FactoryGirl.create(:user) } >> >> ? ? ? before do >> ? ? ? ? fill_in "Email", with: user.email >> ? ? ? ? fill_in "Password", with: user.password >> ? ? ? ? click_button "Sign in" >> ? ? ? end >> >> ? ? ? it { should have_link('Sign out', href: signout_path) } >> ? ? ? it { should_not have_link('Sign in', href: signin_path) } >> ? ? end >> >> >> Now in my other controllers that assume the user is signed in, how can I >> refactor this and put it somewhere that I can just call to make the user >> signed in so I can test pages that assume the user is already signed in? > > Here's one pattern I've seen (and used): > > def login_as(user) > ?fill_in "Email", with: user.email > ?fill_in "Password", with: user.password > ?click_button "Sign in" > end > > describe "things" do > ?before { sign_in_as(FactoryGirl.create:(user) } > ?describe "GET /thing" do > ? ?# ... > ?end > end Where #sign_in_as and #login_as in David's example should be the same method. Zach > > HTH, > David > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -- -- @zachdennis http://www.continuousthinking.com http://www.mutuallyhuman.com From samwgoldman at gmail.com Fri Mar 9 15:24:42 2012 From: samwgoldman at gmail.com (Sam Goldman) Date: Fri, 9 Mar 2012 10:24:42 -0500 Subject: [rspec-users] how to refactor signin process for re-use? In-Reply-To: References: Message-ID: We have used shared contexts to achieve this. https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-context For controller specs using devise, assuming we have let(:current_user) in the test including this shared context: shared_context :signed_in do before do sign_in(current_user) controller.stub!(:current_user).and_return(current_user) end end On Thu, Mar 8, 2012 at 6:15 PM, S Ahmed wrote: > In my authenticate_pages.spec (requests) I do the following to test if the > signin worked: > > ?describe "with valid information" do > ? ? ? #let(:account) { FactoryGirl.create(:account) } > ? ? ? let(:user) { FactoryGirl.create(:user) } > > ? ? ? before do > ? ? ? ? fill_in "Email", with: user.email > ? ? ? ? fill_in "Password", with: user.password > ? ? ? ? click_button "Sign in" > ? ? ? end > > ? ? ? it { should have_link('Sign out', href: signout_path) } > ? ? ? it { should_not have_link('Sign in', href: signin_path) } > ? ? end > > > Now in my other controllers that assume the user is signed in, how can I > refactor this and put it somewhere that I can just call to make the user > signed in so I can test pages that assume the user is already signed in? > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From zach.dennis at gmail.com Fri Mar 9 14:59:02 2012 From: zach.dennis at gmail.com (Zach Dennis) Date: Fri, 9 Mar 2012 09:59:02 -0500 Subject: [rspec-users] Sufficiently tested? In-Reply-To: References: Message-ID: On Fri, Mar 9, 2012 at 8:06 AM, U. M. wrote: > Hi everybody, Hello! > > I have a general question about how many different cases have to be > covered in one Example. > > Considering the famous Fizzbuzz program, you should test if your output > is either "Fizz", "buzz", "Fizzbuzz" or just the number (between 1 and > 100). Pretty simple. However, I often see solutions of testing multiples > of 3 only by looking at some relevant values like 6, 9 etc. > > it "should return 'fizz' when number is divisible by 3" do > ? ?@fizzbuzz.calculate(3).should == 'fizz' > ? ?@fizzbuzz.calculate(6).should == 'fizz' > ? ?@fizzbuzz.calculate(9).should == 'fizz' > end > > So far, so good. But what if I claim that the test will fail in case of > 12? Then add an example for this and make that change. I might update the spec to look like this: describe "#calculate" do it "returns 'fizz' when number is divisible by 3" context "(exceptions to the divisible by 3 rule)" do it "returns nil when number is 12" end end > It's actually not covered and we are just assuming, that all further > multiples will work as well, because these do. My idea was to check all > multiples right away in a loop. > > it "should return Fizz if number is divisible only by 3" do > ?(1..100).each do |number| > ? ?@fizzbuzz.calculate(number).should == "Fizz" if number % 3 == 0 && > ? ?number % 5 != 0 > ?end > end This is more complicated then it needs to be and suffers the same problem that you are trying to avoid. Here you are testing a larger data set, but numbers are infinite... so while 100 numbers is more than 3, it's still a far cry from covering a large amount of the available set of numbers (which is infinite). Similar to David, I try to write examples that show the implementation working, but once I am confident that the examples I have are proving that implementation is working then I move on. > > Given that the calculate method works fine, both ways will pass. But my > question is: Does my solution more than it actually needs to or is the > first one in fact a little too lazy? What do you think? Zach From myron.marston at gmail.com Fri Mar 9 15:39:35 2012 From: myron.marston at gmail.com (Myron Marston) Date: Fri, 9 Mar 2012 07:39:35 -0800 (PST) Subject: [rspec-users] Blog post on building an around(:all) hook using fibers Message-ID: <27536277.557.1331307575129.JavaMail.geo-discussion-forums@pbcgf10> RSpec provides an around(:each) hook but no around(:all) hook. I realized recently that you can build an around hook out of separate before/after hooks using fibers, and decided to blog about it [1], using an around(:all) hook as the example. Some of you may find it interesting or even potentially useful. There's a microgem available as a gist [2] as well. Note that I'll probably never use this gem and the :all hooks may be going away at some point [3], so use at your own risk. It's more of a proof-of-concept and a fun exercise than anything else. Myron [1] http://myronmars.to/n/dev-blog/2012/03/building-an-around-hook-using-fibers [2] https://gist.github.com/2005175 [3] https://github.com/rspec/rspec-core/issues/573 -------------- next part -------------- An HTML attachment was scrubbed... URL: From sahmed1020 at gmail.com Sat Mar 10 02:38:01 2012 From: sahmed1020 at gmail.com (S Ahmed) Date: Fri, 9 Mar 2012 21:38:01 -0500 Subject: [rspec-users] how to refactor signin process for re-use? In-Reply-To: References: Message-ID: Could I somehow add the method to rspec so its available everywhere? What would the class be so inside the describe block my method will be available w/o having to include it? I've seen some people open up the class and add methods to it .... class Test::Unit::TestCase On Fri, Mar 9, 2012 at 10:24 AM, Sam Goldman wrote: > We have used shared contexts to achieve this. > > https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-context > > For controller specs using devise, assuming we have let(:current_user) > in the test including this shared context: > > shared_context :signed_in do > before do > sign_in(current_user) > controller.stub!(:current_user).and_return(current_user) > end > end > > On Thu, Mar 8, 2012 at 6:15 PM, S Ahmed wrote: > > In my authenticate_pages.spec (requests) I do the following to test if > the > > signin worked: > > > > describe "with valid information" do > > #let(:account) { FactoryGirl.create(:account) } > > let(:user) { FactoryGirl.create(:user) } > > > > before do > > fill_in "Email", with: user.email > > fill_in "Password", with: user.password > > click_button "Sign in" > > end > > > > it { should have_link('Sign out', href: signout_path) } > > it { should_not have_link('Sign in', href: signin_path) } > > end > > > > > > Now in my other controllers that assume the user is signed in, how can I > > refactor this and put it somewhere that I can just call to make the user > > signed in so I can test pages that assume the user is already signed in? > > > > > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Mon Mar 12 02:10:56 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Sun, 11 Mar 2012 21:10:56 -0500 Subject: [rspec-users] rspec-2.9.0.rc1 is released! Message-ID: rspec-2.9.0.rc1 has been released, with numerous enhancements and bug fixes: ### rspec-core-2.9.0.rc1 / 2012-03-11 [full changelog](http://github.com/rspec/rspec-core/compare/v2.8.0...v2.9.0.rc1) Enhancements * Support for "X minutes X seconds" spec run duration in formatter. (uzzz) * Strip whitespace from group and example names in doc formatter. * Removed spork-0.9 shim. If you're using spork-0.8.x, you'll need to upgrade to 0.9.0. Bug fixes * Restore `--full_backtrace` option * Ensure that values passed to `config.filter_run` are respected when running over DRb (using spork). * Ensure shared example groups are reset after a run (as example groups are). * Remove `rescue false` from calls to filters represented as Procs * Ensure described_class gets the closest constant (pyromaniac) * In "autorun", don't run the specs in the at_exit hook if there was an exception (most likely due to a SyntaxError). (sunaku) * Don't extend groups with modules already used to extend ancestor groups. ### rspec-expectations-2.9.0.rc1 / 2012-03-11 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0...v2.9.0.rc1) Enhancements * Move built-in matcher classes to RSpec::Matchers::BuiltIn to reduce pollution of RSpec::Matchers (which is included in every example). * Autoload files with matcher classes to improve load time. Bug fixes * Align respond_to? and method_missing in DSL-defined matchers. * Clear out user-defined instance variables between invocations of DSL-defined matchers. * Dup the instance of a DSL generated matcher so its state is not changed by subsequent invocations. * Treat expected args consistently across positive and negative expectations (thanks to Ralf Kistner for the heads up) ### rspec-mocks-2.9.0.rc1 / 2012-03-11 [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.8.0...v2.9.0.rc1) Enhancements * Support order constraints across objects (preethiramdev) Bug fixes * Allow a `as_null_object` to be passed to `with` * Pass proc to block passed to stub (Aubrey Rhodes) * Initialize child message expectation args to match any args (#109 - preethiramdev) ### rspec-rails-2.9.0.rc1 / 2012-03-11 [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.8.1...v2.9.0.rc1) Enhancments * add description method to RouteToMatcher (John Wulff) * Run "db:test:clone_structure" instead of "db:test:prepare" if Active Record's schema format is ":sql". (Andrey Voronkov) Bug fixes * mock_model(XXX).as_null_object.unknown_method returns self again * Generated view specs use different IDs for each attribute. From dchelimsky at gmail.com Mon Mar 12 13:21:42 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 12 Mar 2012 08:21:42 -0500 Subject: [rspec-users] rspec-2.9.0.rc1 is released! In-Reply-To: References: Message-ID: There was an error in one of the gemspecs so I just released 2.9.0.rc2. No other changes since rc1. Cheers, David On Sun, Mar 11, 2012 at 9:10 PM, David Chelimsky wrote: > rspec-2.9.0.rc1 has been released, with numerous enhancements and bug fixes: > > ### rspec-core-2.9.0.rc1 / 2012-03-11 > [full changelog](http://github.com/rspec/rspec-core/compare/v2.8.0...v2.9.0.rc1) > > Enhancements > > * Support for "X minutes X seconds" spec run duration in formatter. (uzzz) > * Strip whitespace from group and example names in doc formatter. > * Removed spork-0.9 shim. If you're using spork-0.8.x, you'll need to upgrade > ?to 0.9.0. > > Bug fixes > > * Restore `--full_backtrace` option > * Ensure that values passed to `config.filter_run` are respected when running > ?over DRb (using spork). > * Ensure shared example groups are reset after a run (as example groups are). > * Remove `rescue false` from calls to filters represented as Procs > * Ensure described_class gets the closest constant (pyromaniac) > * In "autorun", don't run the specs in the at_exit hook if there was an > ?exception (most likely due to a SyntaxError). (sunaku) > * Don't extend groups with modules already used to extend ancestor groups. > > > ### rspec-expectations-2.9.0.rc1 / 2012-03-11 > [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0...v2.9.0.rc1) > > Enhancements > > * Move built-in matcher classes to RSpec::Matchers::BuiltIn to reduce pollution > ?of RSpec::Matchers (which is included in every example). > * Autoload files with matcher classes to improve load time. > > Bug fixes > > * Align respond_to? and method_missing in DSL-defined matchers. > * Clear out user-defined instance variables between invocations of DSL-defined > ?matchers. > * Dup the instance of a DSL generated matcher so its state is not changed by > ?subsequent invocations. > * Treat expected args consistently across positive and negative expectations > ?(thanks to Ralf Kistner for the heads up) > > > ### rspec-mocks-2.9.0.rc1 / 2012-03-11 > [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.8.0...v2.9.0.rc1) > > Enhancements > > * Support order constraints across objects (preethiramdev) > > Bug fixes > > * Allow a `as_null_object` to be passed to `with` > * Pass proc to block passed to stub (Aubrey Rhodes) > * Initialize child message expectation args to match any args (#109 - > ?preethiramdev) > > > ### rspec-rails-2.9.0.rc1 / 2012-03-11 > [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.8.1...v2.9.0.rc1) > > Enhancments > > * add description method to RouteToMatcher (John Wulff) > * Run "db:test:clone_structure" instead of "db:test:prepare" if Active Record's > ?schema format is ":sql". (Andrey Voronkov) > > Bug fixes > > * mock_model(XXX).as_null_object.unknown_method returns self again > * Generated view specs use different IDs for each attribute. > From husseini.mel at gmail.com Tue Mar 13 23:55:35 2012 From: husseini.mel at gmail.com (Mohamad El-Husseini) Date: Tue, 13 Mar 2012 16:55:35 -0700 (PDT) Subject: [rspec-users] Can some one please explain why one of those two examples fails? Message-ID: <4496136.618.1331682935545.JavaMail.geo-discussion-forums@vbga3> The following are what I believe two ways of doing the same thing. Only the first example fails, while the latter passes. I'm finding it hard to get to understand describe and context blocks, particularly with respect to scopes. I would appreciate any "for dummies" explanation, or a link to a blog post that can clear this up for me. I'm still new to Rails and Ruby. describe "send password reset" do let(:user) { FactoryGirl.create(:user) } # This fails context "generates a unique password_reset_token each time" do let(:user) { FactoryGirl.create(:user) } before do user.send_password_reset last_token = user.password_reset_token user.send_password_reset end its(:password_reset_token) { should_not == last_token } end # This passes it "generates a unique password_reset_token each time" do user.send_password_reset last_token = user.password_reset_token user.send_password_reset user.password_reset_token.should_not == last_token end end The first example fails with this: Failure/Error: its(:password_reset_token) { should_not == last_token } NameError: undefined local variable or method `last_token' for # Earlier, it out puts this: should not When you call a matcher in an example without a String, like this: specify { object.should matcher } or this: it { should matcher } RSpec expects the matcher to have a #description method. You should either add a String to the example this matcher is being used in, or give it a description method. Then you won't have to suffer this lengthy warning again. (FAILED - 1) Another thing I noticed is that I can not use capybara inside of describe blocks unless the calls are in a before block... but I don't understand why. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mmazur at gmail.com Wed Mar 14 00:24:03 2012 From: mmazur at gmail.com (Mike Mazur) Date: Wed, 14 Mar 2012 08:24:03 +0800 Subject: [rspec-users] Can some one please explain why one of those two examples fails? In-Reply-To: <4496136.618.1331682935545.JavaMail.geo-discussion-forums@vbga3> References: <4496136.618.1331682935545.JavaMail.geo-discussion-forums@vbga3> Message-ID: Hi, On Wed, Mar 14, 2012 at 07:55, Mohamad El-Husseini wrote: > The following are what I believe two ways of doing the same thing. Only the > first example fails, while the latter passes. In your failing example: context "generates a unique password_reset_token each time" do let(:user) { FactoryGirl.create(:user) } before do user.send_password_reset last_token = user.password_reset_token user.send_password_reset end its(:password_reset_token) { should_not == last_token } end The `last_token` variable is in scope in the before block, but not in the its block. You can fix this by changing it to an instance variable: context "generates a unique password_reset_token each time" do let(:user) { FactoryGirl.create(:user) } before do user.send_password_reset @last_token = user.password_reset_token user.send_password_reset end its(:password_reset_token) { should_not == @last_token } end Another gotcha is: what is the its expression making assertions on? The its method requires a subject to be defined. RSpec defines an implicit subject based on the described class. I imagine at the top of your .spec file you have something like: describe User do # stuff end RSpec will generate a subject by calling `User.new`. `password_reset_token` is then called on this new user instance in the its block. You most certainly wanted to call `password_reset_token` on the user object defined by the `let` statement. So I think this should do the trick: context "generates a unique password_reset_token each time" do let(:user) { FactoryGirl.create(:user) } subject { user } before do user.send_password_reset @last_token = user.password_reset_token user.send_password_reset end its(:password_reset_token) { should_not == @last_token } end You can read more about RSpec's subject here: https://www.relishapp.com/rspec/rspec-core/docs/subject Hope that helps, Mike From srshti at gmail.com Wed Mar 14 07:15:13 2012 From: srshti at gmail.com (Srushti Ambekallu) Date: Wed, 14 Mar 2012 12:45:13 +0530 Subject: [rspec-users] RSpec & JRuby Message-ID: We're working on a project with a bunch of padrino services running on JRuby 1.6.5. Currently, the tests are written Test::Unit, but we want to move to writing specs. We tried rspec but that seemed to fail, until we looked closer and found that JRuby 1.6.6 has fixed whatever the issue was. We are in the process of upgrading to JRuby 1.6.6, which might mean that we can migrate to RSpec. But, I was warned that RSpec has had a number of issues with JRuby in the past and there's no reason not to expect them in the future, and that I should look at MiniTest's specs instead. Do you guys have any issues using RSpec with JRuby? Any advice on the matter would be greatly appreciated. Thanks, Srushti http://rubymonk.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Wed Mar 14 08:22:25 2012 From: jko170 at gmail.com (Justin Ko) Date: Wed, 14 Mar 2012 02:22:25 -0600 Subject: [rspec-users] RSpec & JRuby In-Reply-To: References: Message-ID: On Mar 14, 2012, at 1:15 AM, Srushti Ambekallu wrote: > We're working on a project with a bunch of padrino services running on JRuby 1.6.5. Currently, the tests are written Test::Unit, but we want to move to writing specs. We tried rspec but that seemed to fail, until we looked closer and found that JRuby 1.6.6 has fixed whatever the issue was. > > We are in the process of upgrading to JRuby 1.6.6, which might mean that we can migrate to RSpec. But, I was warned that RSpec has had a number of issues with JRuby in the past and there's no reason not to expect them in the future, and that I should look at MiniTest's specs instead. Do you guys have any issues using RSpec with JRuby? Any advice on the matter would be greatly appreciated. > > Thanks, > Srushti > http://rubymonk.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users Not sure if this helps, but all RSpec libs (with the exception of rspec-mocks) are passing JRuby 1.6.7 on Travis. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at ochsnet.com Wed Mar 14 19:32:01 2012 From: chris at ochsnet.com (Chris Ochs) Date: Wed, 14 Mar 2012 12:32:01 -0700 (PDT) Subject: [rspec-users] controller spec not loading controller classes Message-ID: <17996579.14.1331753521755.JavaMail.geo-discussion-forums@vbnd12> This is jruby 1.6.7, rspec 2, rails 3.0.9. I have a simple controller spec as follows: describe MoneyController, :type => :controller do describe "GET payout" do it "Pays out currency to user" do money = double("Cloud::Money") get :payout end end end Running rake spec results in the following. MoneyController is in app/controllers, but rspec isn't loading any of my controllers. NameError: uninitialized constant MoneyController const_missing at org/jruby/RubyModule.java:2642 const_missing at /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/backward_compatibility.rb:24 (root) at /home/marvel/server/rails/spec/controllers/money_controller_spec.rb:1 load at org/jruby/RubyKernel.java:1058 load_spec_files at /home/marvel/server/rails/spec/controllers/money_controller_spec.rb:698 collect at org/jruby/RubyArray.java:2331 load_spec_files at /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698 run at /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22 run_in_process at /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80 run at /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69 autorun at /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10 Digging deeper, I see that rake spec loads up rails, then quits and then just runs rspec manually? It does this for all types of rspec tests. Wth? Immediately before the above backtrace, this is what I see: /home/marvel/.rvm/rubies/jruby-1.6.7/bin/jruby -S rspec ./spec/test_spec.rb ./spec/controllers/money_controller_spec.rb ./spec/models/dataset_spec.rb Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Wed Mar 14 20:37:44 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 14 Mar 2012 15:37:44 -0500 Subject: [rspec-users] controller spec not loading controller classes In-Reply-To: <17996579.14.1331753521755.JavaMail.geo-discussion-forums@vbnd12> References: <17996579.14.1331753521755.JavaMail.geo-discussion-forums@vbnd12> Message-ID: On Wed, Mar 14, 2012 at 2:32 PM, Chris Ochs wrote: > This is jruby 1.6.7, rspec 2, rails 3.0.9. > > I have a simple controller spec as follows: Does this file require "spec_helper"? If not, that's probably the problem. > describe MoneyController, :type => :controller do > ? describe "GET payout" do > ??? it "Pays out currency to user" do > ????? money = double("Cloud::Money") > ????? get :payout > ??? end > ? end > end > > Running rake spec results in the following.?? MoneyController is in > app/controllers, but rspec isn't loading any of my controllers. > > NameError: uninitialized constant MoneyController > ??? const_missing at org/jruby/RubyModule.java:2642 > ??? const_missing at > /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/backward_compatibility.rb:24 > ?????????? (root) at > /home/marvel/server/rails/spec/controllers/money_controller_spec.rb:1 > ???????????? load at org/jruby/RubyKernel.java:1058 > ? load_spec_files at > /home/marvel/server/rails/spec/controllers/money_controller_spec.rb:698 > ????????? collect at org/jruby/RubyArray.java:2331 > ? load_spec_files at > /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698 > ????????????? run at > /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22 > ?? run_in_process at > /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80 > ????????????? run at > /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69 > ????????? autorun at > /home/marvel/.rvm/gems/jruby-1.6.7/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10 > > > Digging deeper, I see? that rake spec loads up rails, then quits and then > just runs rspec manually? It does this for all types of rspec tests.? Wth? When you run "rake anything" in Rails, you start off in the development environment, so rspec's rake task (and rails' test task) shells out to a new process, allowing spec/spec_helper.rb (or test/test_helper.rb) to set the environment to 'test' (or anything you replace that with). Unfortunately, the end result is you load the rails env twice. You can bypass this by just typing rspec directly. > Immediately before the above backtrace, this is what I see: > > /home/marvel/.rvm/rubies/jruby-1.6.7/bin/jruby -S rspec ./spec/test_spec.rb > ./spec/controllers/money_controller_spec.rb ./spec/models/dataset_spec.rb > > Chris > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From chris at ochsnet.com Wed Mar 14 20:46:49 2012 From: chris at ochsnet.com (Chris Ochs) Date: Wed, 14 Mar 2012 13:46:49 -0700 (PDT) Subject: [rspec-users] controller spec not loading controller classes In-Reply-To: References: <17996579.14.1331753521755.JavaMail.geo-discussion-forums@vbnd12> Message-ID: <16285030.382.1331758010019.JavaMail.geo-discussion-forums@ynjw14> On Wednesday, March 14, 2012 1:37:44 PM UTC-7, dchel... at gmail.com wrote: > > On Wed, Mar 14, 2012 at 2:32 PM, Chris Ochs wrote: > > This is jruby 1.6.7, rspec 2, rails 3.0.9. > > > > I have a simple controller spec as follows: > > Does this file require "spec_helper"? If not, that's probably the problem. > Ok I feel dumb now. I was thinking there had to be something really simple and stupid I was missing, and that's it:) Thanks! Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at ochsnet.com Wed Mar 14 20:46:49 2012 From: chris at ochsnet.com (Chris Ochs) Date: Wed, 14 Mar 2012 13:46:49 -0700 (PDT) Subject: [rspec-users] controller spec not loading controller classes In-Reply-To: References: <17996579.14.1331753521755.JavaMail.geo-discussion-forums@vbnd12> Message-ID: <16285030.382.1331758010019.JavaMail.geo-discussion-forums@ynjw14> On Wednesday, March 14, 2012 1:37:44 PM UTC-7, dchel... at gmail.com wrote: > > On Wed, Mar 14, 2012 at 2:32 PM, Chris Ochs wrote: > > This is jruby 1.6.7, rspec 2, rails 3.0.9. > > > > I have a simple controller spec as follows: > > Does this file require "spec_helper"? If not, that's probably the problem. > Ok I feel dumb now. I was thinking there had to be something really simple and stupid I was missing, and that's it:) Thanks! Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Wed Mar 14 20:57:06 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Wed, 14 Mar 2012 15:57:06 -0500 Subject: [rspec-users] controller spec not loading controller classes In-Reply-To: <16285030.382.1331758010019.JavaMail.geo-discussion-forums@ynjw14> References: <17996579.14.1331753521755.JavaMail.geo-discussion-forums@vbnd12> <16285030.382.1331758010019.JavaMail.geo-discussion-forums@ynjw14> Message-ID: On Wed, Mar 14, 2012 at 3:46 PM, Chris Ochs wrote: > > > On Wednesday, March 14, 2012 1:37:44 PM UTC-7, dchel... at gmail.com wrote: >> >> On Wed, Mar 14, 2012 at 2:32 PM, Chris Ochs wrote: >> > This is jruby 1.6.7, rspec 2, rails 3.0.9. >> > >> > I have a simple controller spec as follows: >> >> Does this file require "spec_helper"? If not, that's probably the problem. > > > Ok I feel dumb now.? I was thinking there had to be something really simple > and stupid I was missing, and that's it:) Happy to help. Cheers, David From husseini.mel at gmail.com Thu Mar 15 12:46:12 2012 From: husseini.mel at gmail.com (Mohamad El-Husseini) Date: Thu, 15 Mar 2012 05:46:12 -0700 (PDT) Subject: [rspec-users] Can some one please explain why one of those two examples fails? In-Reply-To: References: <4496136.618.1331682935545.JavaMail.geo-discussion-forums@vbga3> Message-ID: <17250109.328.1331815572098.JavaMail.geo-discussion-forums@ynel5> Thanks, Mike. I appreciate the explanation. It's tricky knowing what runs when, and what variable is in what scope. It seems like "code smell" to add an instance variable to the before block. I don't understand what advantage one approach has over the other. What would you use, the first, that was broken, or the second? On Tuesday, March 13, 2012 9:24:03 PM UTC-3, Mike Mazur wrote: > > Hi, > > On Wed, Mar 14, 2012 at 07:55, Mohamad El-Husseini > wrote: > > The following are what I believe two ways of doing the same thing. Only > the > > first example fails, while the latter passes. > > In your failing example: > > context "generates a unique password_reset_token each time" do > let(:user) { FactoryGirl.create(:user) } > before do > user.send_password_reset > last_token = user.password_reset_token > user.send_password_reset > end > its(:password_reset_token) { should_not == last_token } > end > > The `last_token` variable is in scope in the before block, but not in > the its block. You can fix this by changing it to an instance > variable: > > context "generates a unique password_reset_token each time" do > let(:user) { FactoryGirl.create(:user) } > before do > user.send_password_reset > @last_token = user.password_reset_token > user.send_password_reset > end > its(:password_reset_token) { should_not == @last_token } > end > > Another gotcha is: what is the its expression making assertions on? > The its method requires a subject to be defined. > > RSpec defines an implicit subject based on the described class. I > imagine at the top of your .spec file you have something like: > > describe User do > # stuff > end > > RSpec will generate a subject by calling `User.new`. > `password_reset_token` is then called on this new user instance in the > its block. You most certainly wanted to call `password_reset_token` on > the user object defined by the `let` statement. > > So I think this should do the trick: > > context "generates a unique password_reset_token each time" do > let(:user) { FactoryGirl.create(:user) } > subject { user } > before do > user.send_password_reset > @last_token = user.password_reset_token > user.send_password_reset > end > its(:password_reset_token) { should_not == @last_token } > end > > You can read more about RSpec's subject here: > > https://www.relishapp.com/rspec/rspec-core/docs/subject > > Hope that helps, > Mike > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zach.dennis at gmail.com Thu Mar 15 14:54:04 2012 From: zach.dennis at gmail.com (Zach Dennis) Date: Thu, 15 Mar 2012 10:54:04 -0400 Subject: [rspec-users] Can some one please explain why one of those two examples fails? In-Reply-To: <17250109.328.1331815572098.JavaMail.geo-discussion-forums@ynel5> References: <4496136.618.1331682935545.JavaMail.geo-discussion-forums@vbga3> <17250109.328.1331815572098.JavaMail.geo-discussion-forums@ynel5> Message-ID: On Thu, Mar 15, 2012 at 8:46 AM, Mohamad El-Husseini wrote: > Thanks, Mike. I appreciate the explanation. It's tricky knowing what runs > when, and what variable is in what scope. It seems like "code smell" to add > an instance variable to the before block. > > I don't understand what advantage one approach has over the other. What > would you use, the first, that was broken, or the second? I am assuming you're asking about what the advantage of using #subject vs. just #user is? If so, here are some advantages of using #subject IMO: RSpec will provide an implicit #subject already and in many cases you don't need to construct a new one. This gets rid of an unnecessary line in those instances. Here's an example from the docs: describe Array do it "should be empty when first created" do subject.should be_empty end end See https://www.relishapp.com/rspec/rspec-core/docs/subject/implicitly-defined-subject for more info. Rspec's one liner syntax uses #subject, straight out of the docs is a great example: describe Array do describe "with 3 items" do subject { [1,2,3] } it { should_not be_empty } end end See https://www.relishapp.com/rspec/rspec-core/v/2-8/docs/subject/implicit-receiver for more information. Also see https://www.relishapp.com/rspec/rspec-core/v/2-8/docs/subject/attribute-of-subject Since #subject is an RSpec convention when writing shared example groups they are often set up to expect subject to be defined by the including example group. Here's an example practically from the docs: shared_examples "a measurable object" do |value| it "should return #{value} from #length" do subject.send(:length).should ==value end end describe Array, "with 3 items" do subject { [1, 2, 3] } it_should_behave_like "a measurable object", 3 end See https://www.relishapp.com/rspec/rspec-core/v/2-8/docs/example-groups/shared-examples for more info. And since #subject is an RSpec convention, it always implies the "thing" under test. So, if you want to know what you're testing you can either do a visual scan for a subject block or simply look at the class/module being described. Those are the reasons I find #subject to have advantage. By themselves, none of these are huge reasons, but combined, and over time, I have found relying on convention and getting a few extra niceties in my specs outweighs going against the convention and trying to reinvent conventions when I hit things like one-liners or shared examples/contexts. My 2 cents, Zach > > > On Tuesday, March 13, 2012 9:24:03 PM UTC-3, Mike Mazur wrote: >> >> Hi, >> >> On Wed, Mar 14, 2012 at 07:55, Mohamad El-Husseini >> wrote: >> > The following are what I believe two ways of doing the same thing. Only >> > the >> > first example fails, while the latter passes. >> >> In your failing example: >> >> ? ?context "generates a unique password_reset_token each time" do >> ? ? ? let(:user) { FactoryGirl.create(:user) } >> ? ? ? before do >> ? ? ? ? user.send_password_reset >> ? ? ? ? last_token = user.password_reset_token >> ? ? ? ? user.send_password_reset >> ? ? ? end >> ? ? ? its(:password_reset_token) { should_not == last_token } >> ? ? end >> >> The `last_token` variable is in scope in the before block, but not in >> the its block. You can fix this by changing it to an instance >> variable: >> >> ? ?context "generates a unique password_reset_token each time" do >> ? ? ? let(:user) { FactoryGirl.create(:user) } >> ? ? ? before do >> ? ? ? ? user.send_password_reset >> ? ? ? ? @last_token = user.password_reset_token >> ? ? ? ? user.send_password_reset >> ? ? ? end >> ? ? ? its(:password_reset_token) { should_not == @last_token } >> ? ? end >> >> Another gotcha is: what is the its expression making assertions on? >> The its method requires a subject to be defined. >> >> RSpec defines an implicit subject based on the described class. I >> imagine at the top of your .spec file you have something like: >> >> ? describe User do >> ? ? # stuff >> ? end >> >> RSpec will generate a subject by calling `User.new`. >> `password_reset_token` is then called on this new user instance in the >> its block. You most certainly wanted to call `password_reset_token` on >> the user object defined by the `let` statement. >> >> So I think this should do the trick: >> >> ? ?context "generates a unique password_reset_token each time" do >> ? ? ? let(:user) { FactoryGirl.create(:user) } >> ? ? ? subject { user } >> ? ? ? before do >> ? ? ? ? user.send_password_reset >> ? ? ? ? @last_token = user.password_reset_token >> ? ? ? ? user.send_password_reset >> ? ? ? end >> ? ? ? its(:password_reset_token) { should_not == @last_token } >> ? ? end >> >> You can read more about RSpec's subject here: >> >> ? https://www.relishapp.com/rspec/rspec-core/docs/subject >> >> Hope that helps, >> Mike >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -- -- @zachdennis http://www.continuousthinking.com http://www.mutuallyhuman.com From rockrep at gmail.com Thu Mar 15 14:53:56 2012 From: rockrep at gmail.com (Michael Kintzer) Date: Thu, 15 Mar 2012 07:53:56 -0700 Subject: [rspec-users] Can some one please explain why one of those two examples fails? In-Reply-To: <4496136.618.1331682935545.JavaMail.geo-discussion-forums@vbga3> References: <4496136.618.1331682935545.JavaMail.geo-discussion-forums@vbga3> Message-ID: <02F826FC-B306-4897-9035-A6D26D7EE301@gmail.com> In your first example, last_token is declared in a before block, which means you need to mark it as an instance variable of the ExampleGroup '@' to reference it in the examples within the context. You don't need to do that when you use let. Also you don't need to declare the redundant let creating a user in that first context, since it was declared outside of the context already. HTH Michael On Mar 13, 2012, at 4:55 PM, Mohamad El-Husseini wrote: > The following are what I believe two ways of doing the same thing. Only the first example fails, while the latter passes. I'm finding it hard to get to understand describe and context blocks, particularly with respect to scopes. I would appreciate any "for dummies" explanation, or a link to a blog post that can clear this up for me. I'm still new to Rails and Ruby. > > describe "send password reset" do > let(:user) { FactoryGirl.create(:user) } > > # This fails > context "generates a unique password_reset_token each time" do > let(:user) { FactoryGirl.create(:user) } > before do > user.send_password_reset > last_token = user.password_reset_token > user.send_password_reset > end > its(:password_reset_token) { should_not == last_token } > end > > # This passes > it "generates a unique password_reset_token each time" do > user.send_password_reset > last_token = user.password_reset_token > user.send_password_reset > user.password_reset_token.should_not == last_token > end > end > > The first example fails with this: > > Failure/Error: its(:password_reset_token) { should_not == last_token } > NameError: > undefined local variable or method `last_token' for # > > Earlier, it out puts this: > > should not When you call a matcher in an example without a String, like this: > > specify { object.should matcher } > > or this: > > it { should matcher } > > RSpec expects the matcher to have a #description method. You should either > add a String to the example this matcher is being used in, or give it a > description method. Then you won't have to suffer this lengthy warning again. > (FAILED - 1) > > > Another thing I noticed is that I can not use capybara inside of describe blocks unless the calls are in a before block... but I don't understand why. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From mmazur at gmail.com Sat Mar 17 11:24:47 2012 From: mmazur at gmail.com (Mike Mazur) Date: Sat, 17 Mar 2012 19:24:47 +0800 Subject: [rspec-users] Can some one please explain why one of those two examples fails? In-Reply-To: <17250109.328.1331815572098.JavaMail.geo-discussion-forums@ynel5> References: <4496136.618.1331682935545.JavaMail.geo-discussion-forums@vbga3> <17250109.328.1331815572098.JavaMail.geo-discussion-forums@ynel5> Message-ID: Hi, On Thu, Mar 15, 2012 at 20:46, Mohamad El-Husseini wrote: > Thanks, Mike. I appreciate the explanation. It's tricky knowing what runs > when, and what variable is in what scope. It seems like "code smell" to add > an instance variable to the before block. > > I don't understand what advantage one approach has over the other. What > would you use, the first, that was broken, or the second? I would probably use `expect`[1]: describe "send password reset" do let(:user) { FactoryGirl.create(:user) } it "generates a unique password_reset_token" do expect { user.send_password_reset }.to change(user, :password_reset_token) end # Or if you want to make sure a token is generated twice it "generates a unique password_reset_token each time" do user.send_password_reset expect { user.send_password_reset }.to change(user, :password_reset_token) end # or perhaps it "generates a unique password_reset_token each time" do 2.times do expect { user.send_password_reset }.to change(user, :password_reset_token) end end end Mike [1]: https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/expect-change From dchelimsky at gmail.com Sat Mar 17 23:13:53 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 17 Mar 2012 18:13:53 -0500 Subject: [rspec-users] rspec-2.9.0 is released! Message-ID: <3EF5D0F8-D00A-4F40-B88A-E35349CD56DA@gmail.com> rspec-2.9.0 is released wtih lots of bug fixes and a few minor feature improvements as well. Enjoy! ### rspec-core-2.9.0 / 2012-03-17 [full changelog](http://github.com/rspec/rspec-core/compare/v2.8.0...v2.9.0) Enhancements * Support for "X minutes X seconds" spec run duration in formatter. (uzzz) * Strip whitespace from group and example names in doc formatter. * Removed spork-0.9 shim. If you're using spork-0.8.x, you'll need to upgrade to 0.9.0. Bug fixes * Restore `--full_backtrace` option * Ensure that values passed to `config.filter_run` are respected when running over DRb (using spork). * Ensure shared example groups are reset after a run (as example groups are). * Remove `rescue false` from calls to filters represented as Procs * Ensure described_class gets the closest constant (pyromaniac) * In "autorun", don't run the specs in the at_exit hook if there was an exception (most likely due to a SyntaxError). (sunaku) * Don't extend groups with modules already used to extend ancestor groups. * `its` correctly memoizes nil or false values (Yamada Masaki) ### rspec-expectations-2.9.0 / 2012-03-17 [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0...v2.9.0) Enhancements * Move built-in matcher classes to RSpec::Matchers::BuiltIn to reduce pollution of RSpec::Matchers (which is included in every example). * Autoload files with matcher classes to improve load time. Bug fixes * Align `respond_to?` and `method_missing` in DSL-defined matchers. * Clear out user-defined instance variables between invocations of DSL-defined matchers. * Dup the instance of a DSL generated matcher so its state is not changed by subsequent invocations. * Treat expected args consistently across positive and negative expectations (thanks to Ralf Kistner for the heads up) ### rspec-mocks-2.9.0 / 2012-03-17 [full changelog](http://github.com/rspec/rspec-mocks/compare/v2.8.0...v2.9.0) Enhancements * Support order constraints across objects (preethiramdev) Bug fixes * Allow a `as_null_object` to be passed to `with` * Pass proc to block passed to stub (Aubrey Rhodes) * Initialize child message expectation args to match any args (#109 - preethiramdev) ### rspec-rails-2.9.0 / 2012-03-17 [full changelog](http://github.com/rspec/rspec-rails/compare/v2.8.1...v2.9.0) Enhancments * add description method to RouteToMatcher (John Wulff) * Run "db:test:clone_structure" instead of "db:test:prepare" if Active Record's schema format is ":sql". (Andrey Voronkov) Bug fixes * `mock_model(XXX).as_null_object.unknown_method` returns self again * Generated view specs use different IDs for each attribute. From myron.marston at gmail.com Sat Mar 17 21:51:25 2012 From: myron.marston at gmail.com (Myron Marston) Date: Sat, 17 Mar 2012 14:51:25 -0700 (PDT) Subject: [rspec-users] Asserting on a yield In-Reply-To: <887CD80B-70C3-4BF6-801E-B890F911D777@gmail.com> References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> <887CD80B-70C3-4BF6-801E-B890F911D777@gmail.com> Message-ID: I've been thinking about this a bit ever since Zach Dennis brought up the issue on another rspec-expectations ticket [1]. I've come up with a proof-of-concept matcher that works pretty well, I think [2]. Here's how you use it: expect { |b| 3.tap(&b) }.to yield_value(3) The argument passed to the expect block is a little weird, given that no other block expectations take a block argument like this. But I don't see a way around it--for the matcher to detect whether or not an argument is yielded (and what the argument is), it needs to control the block passed to the method-under-test. One big win here over some of the other suggestions I've seen is that it works just fine with the current rspec-expectations API (in contrast, some of the other suggestions I've seen would special-case this matcher so that `expect` takes multiple arguments for it to work). I also like that it's built as a block expectation; to me it is in the same category of matchers as the change, raise_error and throw_symbol matchers: it's something that happens as the result of running a bit of code. If enough people like this we can work on getting it into rspec- expectations as an officially supported matcher. If we did, I'd want to beef it up to be significantly more flexible: # for methods that yield multiple arguments to the block... # yield_value would accept a splat of args, and yield_values would # be a more-grammatically-correct alias. expect { |b| foo.fizz(&b) }.to yield_values(:a, 15) # I'd advocate the matcher using the === operator w/ the given values, # so either of these would work expect { |b| "abc".gsub("a", &b) }.to yield_value(String) expect { |b| "abc".gsub("a", &b) }.to yield_value(/a/) # for cases where you need more control over matching the yielded value, pass a block expect { |b| foo.fizz(&b) }.to yield_value { |val| val.should be_fizzy } Thoughts? [1] https://github.com/rspec/rspec-expectations/pull/119#issuecomment-4520633 [2] https://gist.github.com/2065445 From jko170 at gmail.com Sat Mar 17 23:52:57 2012 From: jko170 at gmail.com (Justin Ko) Date: Sat, 17 Mar 2012 17:52:57 -0600 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> <887CD80B-70C3-4BF6-801E-B890F911D777@gmail.com> Message-ID: On Mar 17, 2012, at 3:51 PM, Myron Marston wrote: > I've been thinking about this a bit ever since Zach Dennis brought up > the issue on another rspec-expectations ticket [1]. I've come up with > a proof-of-concept matcher that works pretty well, I think [2]. > Here's how you use it: > > expect { |b| 3.tap(&b) }.to yield_value(3) > > The argument passed to the expect block is a little weird, given that > no other block expectations take a block argument like this. But I > don't see a way around it--for the matcher to detect whether or not an > argument is yielded (and what the argument is), it needs to control > the block passed to the method-under-test. One big win here over some > of the other suggestions I've seen is that it works just fine with the > current rspec-expectations API (in contrast, some of the other > suggestions I've seen would special-case this matcher so that `expect` > takes multiple arguments for it to work). I also like that it's built > as a block expectation; to me it is in the same category of matchers > as the change, raise_error and throw_symbol matchers: it's something > that happens as the result of running a bit of code. > > If enough people like this we can work on getting it into rspec- > expectations as an officially supported matcher. If we did, I'd want > to beef it up to be significantly more flexible: > > # for methods that yield multiple arguments to the block... > # yield_value would accept a splat of args, and yield_values would > # be a more-grammatically-correct alias. > expect { |b| foo.fizz(&b) }.to yield_values(:a, 15) > > # I'd advocate the matcher using the === operator w/ the given values, > # so either of these would work > expect { |b| "abc".gsub("a", &b) }.to yield_value(String) > expect { |b| "abc".gsub("a", &b) }.to yield_value(/a/) > > # for cases where you need more control over matching the yielded > value, pass a block > expect { |b| foo.fizz(&b) }.to yield_value { |val| val.should > be_fizzy } Love this. > > Thoughts? > > [1] https://github.com/rspec/rspec-expectations/pull/119#issuecomment-4520633 > [2] https://gist.github.com/2065445 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From luke at lukemelia.com Sun Mar 18 00:40:35 2012 From: luke at lukemelia.com (Luke Melia) Date: Sat, 17 Mar 2012 20:40:35 -0400 Subject: [rspec-users] rspec-2.9.0 is released! In-Reply-To: <3EF5D0F8-D00A-4F40-B88A-E35349CD56DA@gmail.com> References: <3EF5D0F8-D00A-4F40-B88A-E35349CD56DA@gmail.com> Message-ID: An HTML attachment was scrubbed... URL: From zach.dennis at gmail.com Sun Mar 18 04:35:39 2012 From: zach.dennis at gmail.com (Zach Dennis) Date: Sun, 18 Mar 2012 00:35:39 -0400 Subject: [rspec-users] Asserting on a yield In-Reply-To: References: <158B0125-4E9E-4228-91A5-B48202FBB44E@gmail.com> <887CD80B-70C3-4BF6-801E-B890F911D777@gmail.com> Message-ID: On Sat, Mar 17, 2012 at 7:52 PM, Justin Ko wrote: > > On Mar 17, 2012, at 3:51 PM, Myron Marston wrote: > >> I've been thinking about this a bit ever since Zach Dennis brought up >> the issue on another rspec-expectations ticket [1]. I've come up with >> a proof-of-concept matcher that works pretty well, I think [2]. >> Here's how you use it: >> >> expect { |b| 3.tap(&b) }.to yield_value(3) Thanks for thinking about this some more. I don't think this will work but I've commented on the Gist with a couple examples which help help narrow in the implementation. St. Patty's night can be rough on the mind so I'm not going to pretend that I can code right now. I'll follow up tomorrow if someone doesn't beat me to it! Zach >> >> The argument passed to the expect block is a little weird, given that >> no other block expectations take a block argument like this. ?But I >> don't see a way around it--for the matcher to detect whether or not an >> argument is yielded (and what the argument is), it needs to control >> the block passed to the method-under-test. ?One big win here over some >> of the other suggestions I've seen is that it works just fine with the >> current rspec-expectations API (in contrast, some of the other >> suggestions I've seen would special-case this matcher so that `expect` >> takes multiple arguments for it to work). ?I also like that it's built >> as a block expectation; to me it is in the same category of matchers >> as the change, raise_error and throw_symbol matchers: it's something >> that happens as the result of running a bit of code. >> >> If enough people like this we can work on getting it into rspec- >> expectations as an officially supported matcher. ?If we did, I'd want >> to beef it up to be significantly more flexible: >> >> # for methods that yield multiple arguments to the block... >> # yield_value would accept a splat of args, and yield_values would >> # be a more-grammatically-correct alias. >> expect { |b| foo.fizz(&b) }.to yield_values(:a, 15) >> >> # I'd advocate the matcher using the === operator w/ the given values, >> # so either of these would work >> expect { |b| "abc".gsub("a", &b) }.to yield_value(String) >> expect { |b| "abc".gsub("a", &b) }.to yield_value(/a/) >> >> # for cases where you need more control over matching the yielded >> value, pass a block >> expect { |b| foo.fizz(&b) }.to yield_value { |val| val.should >> be_fizzy } > > Love this. > >> >> Thoughts? >> >> [1] https://github.com/rspec/rspec-expectations/pull/119#issuecomment-4520633 >> [2] https://gist.github.com/2065445 >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -- -- @zachdennis http://www.continuousthinking.com http://www.mutuallyhuman.com From ankita.gupta at adslot.com Tue Mar 20 04:53:51 2012 From: ankita.gupta at adslot.com (Ankita) Date: Mon, 19 Mar 2012 21:53:51 -0700 (PDT) Subject: [rspec-users] issue with running of following code Message-ID: <729838d6-1402-47df-8d73-845c3eb4685c@vy9g2000pbc.googlegroups.com> I am trying to run following file, it does nothing and even does not give any error message.. not sure what is the problem. require "rubygems" require 'watir-webdriver' require "rspec" describe "ASP TEST Suite" do before(:all) do client = Selenium::WebDriver::Remote::Http::Default.new client.timeout = 230 # seconds ? default is 60 profile = Selenium::WebDriver::Firefox::Profile.new driver = Selenium::WebDriver.for(:ff, {:http_client => client, :profile => profile}) $b = Watir::Browser.new(driver) end it "first" do puts "test" end after(:all) do $b.close end end From ankita.gupta at adslot.com Tue Mar 20 04:49:53 2012 From: ankita.gupta at adslot.com (Ankita) Date: Mon, 19 Mar 2012 21:49:53 -0700 (PDT) Subject: [rspec-users] Problem with before(:all) command Message-ID: <0e99f515-cd36-43a1-923a-988318f68053@qg3g2000pbc.googlegroups.com> Hi when I use to run following file, it does not do anything, no error and nothing. It is meant to open a browser and put some information but nothing happens. Any help would be really appreciated... require "rubygems" require 'watir-webdriver' require "rspec" describe "TEST Suite" do before(:all) do client = Selenium::WebDriver::Remote::Http::Default.new client.timeout = 230 # seconds ? default is 60 profile = Selenium::WebDriver::Firefox::Profile.new driver = Selenium::WebDriver.for(:ff, {:http_client => client, :profile => profile}) $b = Watir::Browser.new(driver) end it "first" do puts "test" end it "second" do puts "test" end after(:all) do $b.close end end From dchelimsky at gmail.com Tue Mar 20 11:01:29 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Tue, 20 Mar 2012 06:01:29 -0500 Subject: [rspec-users] issue with running of following code In-Reply-To: <729838d6-1402-47df-8d73-845c3eb4685c@vy9g2000pbc.googlegroups.com> References: <729838d6-1402-47df-8d73-845c3eb4685c@vy9g2000pbc.googlegroups.com> Message-ID: On Mon, Mar 19, 2012 at 11:53 PM, Ankita wrote: > I am trying to run following file, it does nothing and even does not > give any error message.. not sure what is the problem. How are you running it? > require "rubygems" > require 'watir-webdriver' > require "rspec" > > > describe "ASP TEST Suite" do > ?before(:all) do > ? ?client = Selenium::WebDriver::Remote::Http::Default.new > ? ? ? ?client.timeout = 230 # seconds ? default is 60 > ? ? ? ?profile = Selenium::WebDriver::Firefox::Profile.new > ? ? ? ?driver = Selenium::WebDriver.for(:ff, {:http_client => > client, :profile => profile}) > ? ? ? ?$b = Watir::Browser.new(driver) > ?end > it "first" do > ? ? ? ?puts "test" > end > ? after(:all) do > ? ?$b.close > ?end > end > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From ankita.gupta at adslot.com Tue Mar 20 22:45:57 2012 From: ankita.gupta at adslot.com (Ankita) Date: Tue, 20 Mar 2012 15:45:57 -0700 (PDT) Subject: [rspec-users] issue with running of following code In-Reply-To: References: <729838d6-1402-47df-8d73-845c3eb4685c@vy9g2000pbc.googlegroups.com> Message-ID: <9f155604-8478-4224-9d3d-67b01ea14032@pg2g2000pbb.googlegroups.com> I am running by going into specific file folder on command prompt and writing ruby file_name.rb to run it... On Mar 20, 10:01?pm, David Chelimsky wrote: > On Mon, Mar 19, 2012 at 11:53 PM, Ankita wrote: > > I am trying to run following file, it does nothing and even does not > > give any error message.. not sure what is the problem. > > How are you running it? > > > > > > > > > > > require "rubygems" > > require 'watir-webdriver' > > require "rspec" > > > describe "ASP TEST Suite" do > > ?before(:all) do > > ? ?client = Selenium::WebDriver::Remote::Http::Default.new > > ? ? ? ?client.timeout = 230 # seconds ? default is 60 > > ? ? ? ?profile = Selenium::WebDriver::Firefox::Profile.new > > ? ? ? ?driver = Selenium::WebDriver.for(:ff, {:http_client => > > client, :profile => profile}) > > ? ? ? ?$b = Watir::Browser.new(driver) > > ?end > > it "first" do > > ? ? ? ?puts "test" > > end > > ? after(:all) do > > ? ?$b.close > > ?end > > end > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From zach.dennis at gmail.com Wed Mar 21 00:20:03 2012 From: zach.dennis at gmail.com (Zach Dennis) Date: Tue, 20 Mar 2012 20:20:03 -0400 Subject: [rspec-users] issue with running of following code In-Reply-To: <9f155604-8478-4224-9d3d-67b01ea14032@pg2g2000pbb.googlegroups.com> References: <729838d6-1402-47df-8d73-845c3eb4685c@vy9g2000pbc.googlegroups.com> <9f155604-8478-4224-9d3d-67b01ea14032@pg2g2000pbb.googlegroups.com> Message-ID: On Tue, Mar 20, 2012 at 6:45 PM, Ankita wrote: > I am running by going into specific file folder on command prompt and > writing ruby file_name.rb to run it... You'll see less headache if use the "rspec" command instead of ruby to run your specs: rspec file_name_spec.rb Or, is there a reason why you want to invoke rspec without the rspec command? Zach > > On Mar 20, 10:01?pm, David Chelimsky wrote: >> On Mon, Mar 19, 2012 at 11:53 PM, Ankita wrote: >> > I am trying to run following file, it does nothing and even does not >> > give any error message.. not sure what is the problem. >> >> How are you running it? >> >> >> >> >> >> >> >> >> >> > require "rubygems" >> > require 'watir-webdriver' >> > require "rspec" >> >> > describe "ASP TEST Suite" do >> > ?before(:all) do >> > ? ?client = Selenium::WebDriver::Remote::Http::Default.new >> > ? ? ? ?client.timeout = 230 # seconds ? default is 60 >> > ? ? ? ?profile = Selenium::WebDriver::Firefox::Profile.new >> > ? ? ? ?driver = Selenium::WebDriver.for(:ff, {:http_client => >> > client, :profile => profile}) >> > ? ? ? ?$b = Watir::Browser.new(driver) >> > ?end >> > it "first" do >> > ? ? ? ?puts "test" >> > end >> > ? after(:all) do >> > ? ?$b.close >> > ?end >> > end >> > _______________________________________________ >> > rspec-users mailing list >> > rspec-us... at rubyforge.org >> >http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users -- -- @zachdennis http://www.continuousthinking.com http://www.mutuallyhuman.com From ankita.gupta at adslot.com Wed Mar 21 00:58:00 2012 From: ankita.gupta at adslot.com (Ankita) Date: Tue, 20 Mar 2012 17:58:00 -0700 (PDT) Subject: [rspec-users] issue with running of following code In-Reply-To: References: <729838d6-1402-47df-8d73-845c3eb4685c@vy9g2000pbc.googlegroups.com> <9f155604-8478-4224-9d3d-67b01ea14032@pg2g2000pbb.googlegroups.com> Message-ID: <7f1ad477-47e1-4935-8919-15a7ac0afd60@x10g2000pbi.googlegroups.com> It does work by running with rspec file_name_spec.rb. Thanks for that! There is another issue, I wrote my tests in watir and there is following command which use to run pretty well with Watir but now it gives error of undefined method 'confirm' $b.confirm(true) do $b.button(:value,"reload").when_present.fire_event'onclick' # Reset the Database end I used this command to click on a button which use to give some java script pop up and if you click on that pop up use to take a while scrubbing my database. Not sure how I could still use this command. On Mar 21, 11:20?am, Zach Dennis wrote: > On Tue, Mar 20, 2012 at 6:45 PM, Ankita wrote: > > I am running by going into specific file folder on command prompt and > > writing ruby file_name.rb to run it... > > You'll see less headache if use the "rspec" command instead of ruby to > run your specs: > > ? ?rspec file_name_spec.rb > > Or, is there a reason why you want to invoke rspec without the rspec command? > > Zach > > > > > > > > > > > > > On Mar 20, 10:01?pm, David Chelimsky wrote: > >> On Mon, Mar 19, 2012 at 11:53 PM, Ankita wrote: > >> > I am trying to run following file, it does nothing and even does not > >> > give any error message.. not sure what is the problem. > > >> How are you running it? > > >> > require "rubygems" > >> > require 'watir-webdriver' > >> > require "rspec" > > >> > describe "ASP TEST Suite" do > >> > ?before(:all) do > >> > ? ?client = Selenium::WebDriver::Remote::Http::Default.new > >> > ? ? ? ?client.timeout = 230 # seconds ? default is 60 > >> > ? ? ? ?profile = Selenium::WebDriver::Firefox::Profile.new > >> > ? ? ? ?driver = Selenium::WebDriver.for(:ff, {:http_client => > >> > client, :profile => profile}) > >> > ? ? ? ?$b = Watir::Browser.new(driver) > >> > ?end > >> > it "first" do > >> > ? ? ? ?puts "test" > >> > end > >> > ? after(:all) do > >> > ? ?$b.close > >> > ?end > >> > end > >> > _______________________________________________ > >> > rspec-users mailing list > >> > rspec-us... at rubyforge.org > >> >http://rubyforge.org/mailman/listinfo/rspec-users > > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > -- > > -- > @zachdennishttp://www.continuousthinking.comhttp://www.mutuallyhuman.com > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From ankita.gupta at adslot.com Wed Mar 21 01:31:15 2012 From: ankita.gupta at adslot.com (Ankita) Date: Tue, 20 Mar 2012 18:31:15 -0700 (PDT) Subject: [rspec-users] issue with running of following code In-Reply-To: <7f1ad477-47e1-4935-8919-15a7ac0afd60@x10g2000pbi.googlegroups.com> References: <729838d6-1402-47df-8d73-845c3eb4685c@vy9g2000pbc.googlegroups.com> <9f155604-8478-4224-9d3d-67b01ea14032@pg2g2000pbb.googlegroups.com> <7f1ad477-47e1-4935-8919-15a7ac0afd60@x10g2000pbi.googlegroups.com> Message-ID: <221aaa37-887f-4b8c-9119-e515adc5b87e@oq7g2000pbb.googlegroups.com> the error I got when I run the script using rspec file_name_spec.rb .... when I used to run using ruby file_name_spec.rb it was fine On Mar 21, 11:58?am, Ankita wrote: > It does work ?by running with ?rspec file_name_spec.rb. Thanks for > that! > > There is another issue, I wrote my tests in watir and there is > following command which use to run pretty well with Watir but now it > gives error of undefined method 'confirm' > > $b.confirm(true) do > ? ? ? $b.button(:value,"reload").when_present.fire_event'onclick' # > Reset the Database > ? ? end > > I used this command to click on a button which use to give some java > script pop up and if you click on that pop up use to take a while > scrubbing my database. > Not sure how I could still use this command. > > On Mar 21, 11:20?am, Zach Dennis wrote: > > > > > > > > > On Tue, Mar 20, 2012 at 6:45 PM, Ankita wrote: > > > I am running by going into specific file folder on command prompt and > > > writing ruby file_name.rb to run it... > > > You'll see less headache if use the "rspec" command instead of ruby to > > run your specs: > > > ? ?rspec file_name_spec.rb > > > Or, is there a reason why you want to invoke rspec without the rspec command? > > > Zach > > > > On Mar 20, 10:01?pm, David Chelimsky wrote: > > >> On Mon, Mar 19, 2012 at 11:53 PM, Ankita wrote: > > >> > I am trying to run following file, it does nothing and even does not > > >> > give any error message.. not sure what is the problem. > > > >> How are you running it? > > > >> > require "rubygems" > > >> > require 'watir-webdriver' > > >> > require "rspec" > > > >> > describe "ASP TEST Suite" do > > >> > ?before(:all) do > > >> > ? ?client = Selenium::WebDriver::Remote::Http::Default.new > > >> > ? ? ? ?client.timeout = 230 # seconds ? default is 60 > > >> > ? ? ? ?profile = Selenium::WebDriver::Firefox::Profile.new > > >> > ? ? ? ?driver = Selenium::WebDriver.for(:ff, {:http_client => > > >> > client, :profile => profile}) > > >> > ? ? ? ?$b = Watir::Browser.new(driver) > > >> > ?end > > >> > it "first" do > > >> > ? ? ? ?puts "test" > > >> > end > > >> > ? after(:all) do > > >> > ? ?$b.close > > >> > ?end > > >> > end > > >> > _______________________________________________ > > >> > rspec-users mailing list > > >> > rspec-us... at rubyforge.org > > >> >http://rubyforge.org/mailman/listinfo/rspec-users > > > >> _______________________________________________ > > >> rspec-users mailing list > > >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-us... at rubyforge.org > > >http://rubyforge.org/mailman/listinfo/rspec-users > > > -- > > > -- > > @zachdennishttp://www.continuousthinking.comhttp://www.mutuallyhuman.com > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users From tyler.scott.dewitt at gmail.com Wed Mar 21 01:01:08 2012 From: tyler.scott.dewitt at gmail.com (Tyler DeWitt) Date: Tue, 20 Mar 2012 18:01:08 -0700 Subject: [rspec-users] Testing Model Attributes That Are Updated In a Controller Message-ID: I've got a controller that updates some attributes of a model: def confirmation @cart = current_cart customer = Stripe::Customer.create(description: current_user.email, card: params[:stripeToken]) current_user.update_attributes(stripe_customer_id: customer.id) end In my spec, how can I ensure the current_user has a stripe_customer_id after this controller is run? I have this: it "should create a stripe customer and save that to the stripe_customer_id of the user" do @user.reload @user.stripe_customer_id.should_not be_nil end But it doesn't work (I tried @user.reload because the database is being updated by the call to current_user.update_attributes). @user is a FactoryGirl created object further up in the test. Thanks, Tyler From srshti at gmail.com Wed Mar 21 07:55:42 2012 From: srshti at gmail.com (Srushti Ambekallu) Date: Wed, 21 Mar 2012 13:25:42 +0530 Subject: [rspec-users] Testing Model Attributes That Are Updated In a Controller In-Reply-To: References: Message-ID: <01B0DD30-AA45-4424-B070-BBF8DB026D8E@gmail.com> On 21-Mar-2012, at 6:31 AM, Tyler DeWitt wrote: > I've got a controller that updates some attributes of a model: > > def confirmation > @cart = current_cart > customer = Stripe::Customer.create(description: current_user.email, card: params[:stripeToken]) > current_user.update_attributes(stripe_customer_id: customer.id) > end > > In my spec, how can I ensure the current_user has a stripe_customer_id after this controller is run? > > I have this: > > it "should create a stripe customer and save that to the stripe_customer_id of the user" do > @user.reload > @user.stripe_customer_id.should_not be_nil > end > > But it doesn't work (I tried @user.reload because the database is being updated by the call to current_user.update_attributes). > > @user is a FactoryGirl created object further up in the test. > > Thanks, > Tyler > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users It looks like that ought to work. The first thing I'd suggest is to ensure that your request is actually succeeding and not erroring out or getting redirected due to authentication. response.should be_ok Next, I'd confirm that the two users are the same (in spec & in the controller) by 'puts'ing the ids or something. Could you confirm that those two fairly common reasons these sorts of things usually fail for me don't apply? Then we can start looking at other possible issues. Also, I would probably be more specific in my assertion by doing @user.stripe_customer.should == Stripe::Customer.last.id Thanks, Srushti http://c42.in From ankita.gupta at adslot.com Thu Mar 22 04:50:54 2012 From: ankita.gupta at adslot.com (Ankita) Date: Wed, 21 Mar 2012 21:50:54 -0700 (PDT) Subject: [rspec-users] How to handle Java script pop up when you click on a button to perform some action to DB Message-ID: <7351f97c-af1c-466b-b587-947edd12c95e@lf20g2000pbb.googlegroups.com> Hi I have a button which when clicked opens a JS pop up for confirmation, confirming which, basically scrubs my DB and so takes a minute to complete its action. When I was using watir, I use to put in following code which use to work, but when switching to Rspec, it gives error so not sure what should I be replacing here: $b.confirm(true) do $b.button(:value,"Scrub the DB - Full reset / reload").when_present.fire_event'onclick' end From lists at ruby-forum.com Thu Mar 22 14:54:16 2012 From: lists at ruby-forum.com (Pito Salas) Date: Thu, 22 Mar 2012 15:54:16 +0100 Subject: [rspec-users] Rspec not loading fixtures Message-ID: <9fcec270cb5b38b104c8532622639982@ruby-forum.com> I am taking over a code base and am trying to run the tests. I am somewhat new to RSpec so this might be a trivial problem. Basically I can tell that the fixtures are not getting loaded. All 100 tests fail with a similar error. But I don't know why. Below is the code, with my narrative with *** before it... Can you see anything or give me a clue where to look. So far I am coming up empty... Thanks! Pito Salas **** In spec_helper.rb, which I know is running, I see: Spec::Runner.configure do |config| config.global_fixtures = :all end **** One of the tests in spec/controllers/downloads_controller_spec.rb is below. I know it is running and I know that before the 'describe', Partner.count == 0, so no fixture. require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe DownloadsController do integrate_views describe "when PDF is ready" do before(:each) do @registrant = Factory.create(:step_5_registrant) stub(@registrant).merge_pdf { `touch #{@registrant.pdf_file_path}` } @registrant.generate_pdf @registrant.save! end it "provides a link to download the PDF" do get :show, :registrant_id => @registrant.to_param assert_not_nil assigns[:registrant] assert_response :success assert_template "show" assert_select "span.button a[target=_blank]" assert_select "span.button a[onclick]" end after(:each) do `rm #{@registrant.pdf_file_path}` end end **** And here is what in various directories: spec/fixtures/partners.yml - which contains 2 yaml records: sponsor: id: 1 username: rtv email: rocky at example.com crypted_password: "c8e5b51b237344fe0e72539af0cac7197f094a5e933ffacf6e7fa612363c5933f520710c6427ac31fc4c68a2d7bb48eae601c74b96e7838f9ca1a0740b67576a" password_salt: "Y4PPzYx2ert3vC0OhEMo" name: Rocky organization: Rock The Vote url: http://rockthevote.com address: 123 Wherever city: Washington state_id: 9 zip_code: 20001 phone: 555-555-1234 survey_question_1_en: "What school did you go to?" survey_question_2_en: "What is your favorite musical group?" created_at: <%= Time.now %> updated_at: <%= Time.now %> # TODO: remove partner 2 in production partner: id: 2 username: bull_winkle email: bull_winkle at example.com crypted_password: "c8e5b51b237344fe0e72539af0cac7197f094a5e933ffacf6e7fa612363c5933f520710c6427ac31fc4c68a2d7bb48eae601c74b96e7838f9ca1a0740b67576a" password_salt: "Y4PPzYx2ert3vC0OhEMo" name: Bullwinkle organization: Bullwinkle, Inc. url: http://example.com address: 123 Wherever city: Washington state_id: 9 zip_code: 20001 phone: 555-555-1234 survey_question_1_en: "What school did you go to?" survey_question_2_en: "What is your favorite musical group?" created_at: <%= Time.now %> updated_at: <%= Time.now %> -- Posted via http://www.ruby-forum.com/. From lists at ruby-forum.com Thu Mar 22 17:27:23 2012 From: lists at ruby-forum.com (Fearless Fool) Date: Thu, 22 Mar 2012 18:27:23 +0100 Subject: [rspec-users] mocking a reference to a polymorphic model? Message-ID: <1abf30fade0694534ea1d723088f94ed@ruby-forum.com> The basic question: how do you mock a reference to a polymorphic class in RSpec? Trying the obvious thing leads to an error of the form: undefined method `base_class' for Post:Class === The details: # file: app/models/relation.rb class Relation < ActiveRecord::Base belongs_to :entity, :polymorphic => true end # file: db/xxx_create_relations.rb class CreateRelations < ActiveRecord::Migration def change create_table :relations do |t| t.references :entity, :polymorphic => true, :null => false end end end # file: spec/models/relation_spec.rb describe Relation do it 'should create instance without error' do post = mock_model("Post") lambda { Relation.create!(:entity => @post)}.should_not raise_error end end === Running the test: 1) Relation should create instance without error Failure/Error: lambda { Relation.create!(:entity => post) }.should_not raise_error expected no Exception, got # # ./spec/models/relation_spec.rb:39:in `block (2 levels) in ' My hunch is that ActiveRecord is calling base_class on the mocked model (Post) to determine what to stick in the 'entity_type' column in the relations table. I tried explicitly setting it, as in: post = mock_model("Post", :base_class => "Post") but the error was the same. For all the reasons that DC espouses, I'd love to mock references to the polymorphic models, but I don't see how to do that. Any ideas? -- Posted via http://www.ruby-forum.com/. From chabgood at gmail.com Thu Mar 22 18:14:07 2012 From: chabgood at gmail.com (Chris Habgood) Date: Thu, 22 Mar 2012 13:14:07 -0500 Subject: [rspec-users] mocking a reference to a polymorphic model? In-Reply-To: <1abf30fade0694534ea1d723088f94ed@ruby-forum.com> References: <1abf30fade0694534ea1d723088f94ed@ruby-forum.com> Message-ID: Are you really sure you need a polymorphic table? On Thu, Mar 22, 2012 at 12:27, Fearless Fool wrote: > The basic question: how do you mock a reference to a polymorphic class > in RSpec? Trying the obvious thing leads to an error of the form: > > undefined method `base_class' for Post:Class > > === The details: > > # file: app/models/relation.rb > class Relation < ActiveRecord::Base > belongs_to :entity, :polymorphic => true > end > > # file: db/xxx_create_relations.rb > class CreateRelations < ActiveRecord::Migration > def change > create_table :relations do |t| > t.references :entity, :polymorphic => true, :null => false > end > end > end > > # file: spec/models/relation_spec.rb > describe Relation do > it 'should create instance without error' do > post = mock_model("Post") > lambda { Relation.create!(:entity => @post)}.should_not raise_error > end > end > > === Running the test: > > 1) Relation should create instance without error > Failure/Error: lambda { Relation.create!(:entity => post) > }.should_not raise_error > expected no Exception, got # `base_class' for Post:Class> > # ./spec/models/relation_spec.rb:39:in `block (2 levels) in (required)>' > > My hunch is that ActiveRecord is calling base_class on the mocked model > (Post) to determine what to stick in the 'entity_type' column in the > relations table. I tried explicitly setting it, as in: > > post = mock_model("Post", :base_class => "Post") > > but the error was the same. > > For all the reasons that DC espouses, I'd love to mock references to the > polymorphic models, but I don't see how to do that. > > Any ideas? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > -- *"In matters of style, swim with the current; in matters of principle, stand like a rock." Thomas Jefferson * -------------- next part -------------- An HTML attachment was scrubbed... URL: From matt at mattwynne.net Thu Mar 22 20:10:46 2012 From: matt at mattwynne.net (Matt Wynne) Date: Thu, 22 Mar 2012 20:10:46 +0000 Subject: [rspec-users] mocking a reference to a polymorphic model? In-Reply-To: <1abf30fade0694534ea1d723088f94ed@ruby-forum.com> References: <1abf30fade0694534ea1d723088f94ed@ruby-forum.com> Message-ID: <23C0E331-B1FE-4C66-BB5C-719FB377D46F@mattwynne.net> On 22 Mar 2012, at 17:27, Fearless Fool wrote: > The basic question: how do you mock a reference to a polymorphic class > in RSpec? Trying the obvious thing leads to an error of the form: > > undefined method `base_class' for Post:Class > > === The details: > > # file: app/models/relation.rb > class Relation < ActiveRecord::Base > belongs_to :entity, :polymorphic => true > end > > # file: db/xxx_create_relations.rb > class CreateRelations < ActiveRecord::Migration > def change > create_table :relations do |t| > t.references :entity, :polymorphic => true, :null => false > end > end > end > > # file: spec/models/relation_spec.rb > describe Relation do > it 'should create instance without error' do > post = mock_model("Post") > lambda { Relation.create!(:entity => @post)}.should_not raise_error > end > end > > === Running the test: > > 1) Relation should create instance without error > Failure/Error: lambda { Relation.create!(:entity => post) > }.should_not raise_error > expected no Exception, got # `base_class' for Post:Class> > # ./spec/models/relation_spec.rb:39:in `block (2 levels) in (required)>' > > My hunch is that ActiveRecord is calling base_class on the mocked model > (Post) to determine what to stick in the 'entity_type' column in the > relations table. I tried explicitly setting it, as in: > > post = mock_model("Post", :base_class => "Post") > > but the error was the same. > > For all the reasons that DC espouses, I'd love to mock references to the > polymorphic models, but I don't see how to do that. Can you enumerate those reasons? This doesn't seem like a good use-case for mock_model, to me. cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: From alexch at gmail.com Thu Mar 22 21:24:05 2012 From: alexch at gmail.com (Alex Chaffee) Date: Thu, 22 Mar 2012 14:24:05 -0700 (PDT) Subject: [rspec-users] bug: rspec rails generator generates a failing spec In-Reply-To: <16351794.73.1332451144960.JavaMail.geo-discussion-forums@pbbpk10> References: <16351794.73.1332451144960.JavaMail.geo-discussion-forums@pbbpk10> Message-ID: <14403969.98.1332451445249.JavaMail.geo-discussion-forums@pbboc6> Oops. This is apparently not an rspec bug after all... the tutorial instructions generated the StaticPages controller in a non-standard way. Sorry for the false alarm! On Thursday, March 22, 2012 2:19:04 PM UTC-7, Alex Chaffee wrote: > > Using rspec 2.9.0, rspec-rails 2.9.0, rails 3.2.2, following Michael > Hartl's RailsTutorial, version 3.2, chapter 3. > > Running > rails generate integration_test static_pages > makes a spec named static_pages_spec.rb that contains the following code > get static_pages_index_path > which causes this error when run > > 1) StaticPages GET /static_pages works! (now write some real specs) > Failure/Error: get static_pages_index_path > NameError: > undefined local variable or method `static_pages_index_path' for > # > # ./spec/requests/static_pages_?spec.rb:7:in `block (3 levels) in > ' > > Also, I couldn't figure out where or how to report rspec bugs; sorry for > mailing the list if this isn't the right place to do it. > > - A > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alexch at gmail.com Thu Mar 22 21:19:04 2012 From: alexch at gmail.com (Alex Chaffee) Date: Thu, 22 Mar 2012 14:19:04 -0700 (PDT) Subject: [rspec-users] bug: rspec rails generator generates a failing spec Message-ID: <16351794.73.1332451144960.JavaMail.geo-discussion-forums@pbbpk10> Using rspec 2.9.0, rspec-rails 2.9.0, rails 3.2.2, following Michael Hartl's RailsTutorial, version 3.2, chapter 3. Running rails generate integration_test static_pages makes a spec named static_pages_spec.rb that contains the following code get static_pages_index_path which causes this error when run 1) StaticPages GET /static_pages works! (now write some real specs) Failure/Error: get static_pages_index_path NameError: undefined local variable or method `static_pages_index_path' for # # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in ' Also, I couldn't figure out where or how to report rspec bugs; sorry for mailing the list if this isn't the right place to do it. - A -------------- next part -------------- An HTML attachment was scrubbed... URL: From josh at resumecompanion.com Fri Mar 23 08:34:17 2012 From: josh at resumecompanion.com (Josh Moore) Date: Fri, 23 Mar 2012 16:34:17 +0800 Subject: [rspec-users] rspec-rails does execute any tests Message-ID: <2C412947-EC45-471C-A362-70350D08FCD6@resumecompanion.com> I have inherited a rails 2.3.14 application with no tests. I have gotten cucumber working but when I try to user rspec will not run any of the test cases. These are the first four lines in myspec/spec_helper.rb. ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment')) require 'spec/autorun' require 'spec/rails' When I run bundle exec rake spec it does not have any test cases. But, when I remove require 'spec/rails' it runs correctly and the test cases are executed. I have looked at all the code and even started digging into the rspec-rails gem (rspec-rails 1.3.4 and rspec 1.3.2) but have not found anything. Any ideas of what might be going wrong when rspec-rails is used? Josh Moore resumecompanion.com josh at resumecompanion.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Fri Mar 23 12:25:13 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Fri, 23 Mar 2012 07:25:13 -0500 Subject: [rspec-users] rspec-rails does execute any tests In-Reply-To: <2C412947-EC45-471C-A362-70350D08FCD6@resumecompanion.com> References: <2C412947-EC45-471C-A362-70350D08FCD6@resumecompanion.com> Message-ID: On Fri, Mar 23, 2012 at 3:34 AM, Josh Moore wrote: > I??have inherited a rails 2.3.14 application with no tests. I have gotten > cucumber working but when I try to user rspec will not run any of the test > cases. These are the first four lines in myspec/spec_helper.rb. > > ENV["RAILS_ENV"] ||= 'test' > require > File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment')) > require 'spec/autorun' > require 'spec/rails' > > When I run?bundle exec rake spec?it does not have any test cases. But, when > I remove?require 'spec/rails'?it runs correctly and the test cases are > executed. I have looked at all the code and even started digging into the > rspec-rails gem (rspec-rails 1.3.4 and rspec 1.3.2) but have not found > anything. Any ideas of what might be going wrong when rspec-rails is used? What's in the Gemfile? From anexiole at gmail.com Mon Mar 26 10:58:39 2012 From: anexiole at gmail.com (Gordon Yeong) Date: Mon, 26 Mar 2012 21:58:39 +1100 Subject: [rspec-users] 'new' method works as expected but rspec does not recognise flash contents or redirection In-Reply-To: References: Message-ID: Guys, Not sure why but I deleted my Gemfile.lock file. Found that it's due to a stale Gemfile.lock file which is still using rspec-core 2.8.0 when in fact I just performed and update yesterday night on rspec (hence I've got rspec-core 2.9.0). Ran "rspec parts_controller.spec.rb" again and all tests are passing. Could it really be the stale Gemfile.lock file? hmmm On 26 March 2012 21:47, Gordon wrote: > hi all :) > > Found a spec failing today. > Not sure why. > > When I run my application and successfully added a new object, i can > see > 1) a redirection has taken place to the index page as expected (302 > code returned) from the console > 2) the flash notice's message being displayed in the index page > > BUT > when I run the specs (see below), it fails indicating that the > redirection did not occur (as a 200 code was returned) AND > that the flash notice message was nil. > > > ---------specs error start ------------------------------------- > > 1) PartsController saves the new part object successfully sets the > flash with a success message > Failure/Error: flash[:notice].should eq('Part was successfully > created.') > > expected: "Part was successfully created." > got: nil > > (compared using ==) > # ./spec/controllers/parts_controller_spec.rb:35:in `block (3 > levels) in ' > > 2) PartsController saves the new part object successfully redirects > the user back to the parts index page > Failure/Error: response.should redirect_to( :action => 'index' ) > Expected response to be a <:redirect>, but was <200> > # ./spec/controllers/parts_controller_spec.rb:39:in `block (3 > levels) in ' > > --------- specs error end ------------------------------------- > > --------- extract of spec/controllers/parts_controller_spec.rb - start > ------------------ > > context 'saves the new part object successfully' do > before(:each) do > post :create, :part => { 'title' => 'HKS boost > controller' } > end > > # we could combine the 2 specs below to save on the post > request > # to the create action but having 2 separate specs would give > # clarity > it 'sets the flash with a success message' do > flash[:notice].should eq('Part was successfully created.') > end > > it 'redirects the user back to the parts index page' do > response.should redirect_to( :action => 'index' ) > end > end > > --------- extract of spec/controllers/parts_controller_spec.rb - end > ------------------ > > > I'm not sure what's going on :( > > > > > -------- "parts_controller.rb" - start > ------------------------------- > > # POST /parts > # POST /parts.xml > def create > # Record current user's id as he/she created the part > params[:part][:created_by] = current_user.id > params[:part][:updated_by] = current_user.id > > @part = Part.new(params[:part]) > respond_to do |format| > if @part.save > flash[:notice] = 'Part was successfully created.' > format.html { redirect_to(parts_path) } > format.xml { render :xml => @part, :status > => :created, :location => @part } > format.json { > @parts = Part.all; > render :json => @parts > } > else > format.html { render :action => "new" } > format.xml { render :xml => @part.errors, :status > => :unprocessable_entity } > end > end > end > > > -------- "parts_controller.rb" - end ------------------------------- > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From anexiole at gmail.com Mon Mar 26 10:47:33 2012 From: anexiole at gmail.com (Gordon) Date: Mon, 26 Mar 2012 03:47:33 -0700 (PDT) Subject: [rspec-users] 'new' method works as expected but rspec does not recognise flash contents or redirection Message-ID: hi all :) Found a spec failing today. Not sure why. When I run my application and successfully added a new object, i can see 1) a redirection has taken place to the index page as expected (302 code returned) from the console 2) the flash notice's message being displayed in the index page BUT when I run the specs (see below), it fails indicating that the redirection did not occur (as a 200 code was returned) AND that the flash notice message was nil. ---------specs error start ------------------------------------- 1) PartsController saves the new part object successfully sets the flash with a success message Failure/Error: flash[:notice].should eq('Part was successfully created.') expected: "Part was successfully created." got: nil (compared using ==) # ./spec/controllers/parts_controller_spec.rb:35:in `block (3 levels) in ' 2) PartsController saves the new part object successfully redirects the user back to the parts index page Failure/Error: response.should redirect_to( :action => 'index' ) Expected response to be a <:redirect>, but was <200> # ./spec/controllers/parts_controller_spec.rb:39:in `block (3 levels) in ' --------- specs error end ------------------------------------- --------- extract of spec/controllers/parts_controller_spec.rb - start ------------------ context 'saves the new part object successfully' do before(:each) do post :create, :part => { 'title' => 'HKS boost controller' } end # we could combine the 2 specs below to save on the post request # to the create action but having 2 separate specs would give # clarity it 'sets the flash with a success message' do flash[:notice].should eq('Part was successfully created.') end it 'redirects the user back to the parts index page' do response.should redirect_to( :action => 'index' ) end end --------- extract of spec/controllers/parts_controller_spec.rb - end ------------------ I'm not sure what's going on :( -------- "parts_controller.rb" - start ------------------------------- # POST /parts # POST /parts.xml def create # Record current user's id as he/she created the part params[:part][:created_by] = current_user.id params[:part][:updated_by] = current_user.id @part = Part.new(params[:part]) respond_to do |format| if @part.save flash[:notice] = 'Part was successfully created.' format.html { redirect_to(parts_path) } format.xml { render :xml => @part, :status => :created, :location => @part } format.json { @parts = Part.all; render :json => @parts } else format.html { render :action => "new" } format.xml { render :xml => @part.errors, :status => :unprocessable_entity } end end end -------- "parts_controller.rb" - end ------------------------------- From pcasaretto at gmail.com Tue Mar 27 00:20:33 2012 From: pcasaretto at gmail.com (Paulo Luis Franchini Casaretto) Date: Mon, 26 Mar 2012 17:20:33 -0700 (PDT) Subject: [rspec-users] [rspec-rails] Mailer tests should be similar to Controller tests Message-ID: <9862337.609.1332807633683.JavaMail.geo-discussion-forums@vbbfw10> Hey, When I'm testing a controller, I basically test three things. Does it assign the variables the view needs? Does it render the right template? Does it do whatever the action is supposed to do? And when testing mailers I feel it should be basically the same. Does it assign the variables the view needs? Does it render the right template? Does it set the right headers? I don't want to test the actual mail object, the same way I don't want to test the views in controller tests. On the other hand, the mail object IS the output of the mailer, so I don't know. What do you guys think about this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Tue Mar 27 03:27:20 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Mon, 26 Mar 2012 22:27:20 -0500 Subject: [rspec-users] Rspec not loading fixtures In-Reply-To: <9fcec270cb5b38b104c8532622639982@ruby-forum.com> References: <9fcec270cb5b38b104c8532622639982@ruby-forum.com> Message-ID: On Thu, Mar 22, 2012 at 9:54 AM, Pito Salas wrote: > I am taking over a code base and am trying to run the tests. I am > somewhat new to RSpec so this might be a trivial problem. > > Basically I can tell that the fixtures are not getting loaded. All 100 > tests fail with a similar error. > > But I don't know why. ?Below is the code, with my narrative with *** > before it... Can you see anything or give me a clue where to look. So > far I am coming up empty... > > Thanks! > > Pito Salas > > **** In spec_helper.rb, which I know is running, I see: > > Spec::Runner.configure do |config| > ?config.global_fixtures = :all > end > > **** One of the tests in spec/controllers/downloads_controller_spec.rb > is below. I know it is running and I know that before the 'describe', > Partner.count == 0, so no fixture. > > require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') > > describe DownloadsController do > ?integrate_views > > ?describe "when PDF is ready" do > ? ?before(:each) do > ? ? ?@registrant = Factory.create(:step_5_registrant) > ? ? ?stub(@registrant).merge_pdf { `touch #{@registrant.pdf_file_path}` > } > ? ? ?@registrant.generate_pdf > ? ? ?@registrant.save! > ? ?end > > ? ?it "provides a link to download the PDF" do > ? ? ?get :show, :registrant_id => @registrant.to_param > ? ? ?assert_not_nil assigns[:registrant] > ? ? ?assert_response :success > ? ? ?assert_template "show" > ? ? ?assert_select "span.button a[target=_blank]" > ? ? ?assert_select "span.button a[onclick]" > ? ?end > > ? ?after(:each) do > ? ? ?`rm #{@registrant.pdf_file_path}` > ? ?end > ?end > > **** And here is what in various directories: > > spec/fixtures/partners.yml - which contains 2 yaml records: > > sponsor: > ?id: 1 > ?username: rtv > ?email: rocky at example.com > ?crypted_password: > "c8e5b51b237344fe0e72539af0cac7197f094a5e933ffacf6e7fa612363c5933f520710c6427ac31fc4c68a2d7bb48eae601c74b96e7838f9ca1a0740b67576a" > ?password_salt: "Y4PPzYx2ert3vC0OhEMo" > ?name: Rocky > ?organization: Rock The Vote > ?url: http://rockthevote.com > ?address: 123 Wherever > ?city: Washington > ?state_id: 9 > ?zip_code: 20001 > ?phone: 555-555-1234 > ?survey_question_1_en: "What school did you go to?" > ?survey_question_2_en: "What is your favorite musical group?" > ?created_at: <%= Time.now %> > ?updated_at: <%= Time.now %> > # TODO: remove partner 2 in production > partner: > ?id: 2 > ?username: bull_winkle > ?email: bull_winkle at example.com > ?crypted_password: > "c8e5b51b237344fe0e72539af0cac7197f094a5e933ffacf6e7fa612363c5933f520710c6427ac31fc4c68a2d7bb48eae601c74b96e7838f9ca1a0740b67576a" > ?password_salt: "Y4PPzYx2ert3vC0OhEMo" > ?name: Bullwinkle > ?organization: Bullwinkle, Inc. > ?url: http://example.com > ?address: 123 Wherever > ?city: Washington > ?state_id: 9 > ?zip_code: 20001 > ?phone: 555-555-1234 > ?survey_question_1_en: "What school did you go to?" > ?survey_question_2_en: "What is your favorite musical group?" > ?created_at: <%= Time.now %> > ?updated_at: <%= Time.now %> What's the actual failure/error message you're seeing? Also, what happens when you set `fixtures :users` directly in one of the example groups e.g. describe DownloadsController do integrate_views fixtures :users From lists at ruby-forum.com Tue Mar 27 08:40:06 2012 From: lists at ruby-forum.com (Andreas Plath) Date: Tue, 27 Mar 2012 10:40:06 +0200 Subject: [rspec-users] Sufficiently tested? In-Reply-To: References: Message-ID: <9c67556a1f976c14abffe43a74c8ec95@ruby-forum.com> Dear Ulf, in your fizzbuzz example a kind of simple algorithm is used (% 3 and % 5) which can be easily tested in a loop. If this loop is clearly predictable, a test with only three levels like 3, 6, 9 is enough in my opinion. To extend the test from 1 to 100 in this case is a bit to much. Assuming there are exceptions in fizzbuzz like 60 should not be "Fizzbuzz" and 89 should not be "Fizz" your loop would fail. My point is not that your loop fails. My point is You have to create a new algorithm for your loop, taking care of the exceptions for 60 and 89. Keeping the thought of a loop including a special algorithm, one might be tempted to extend the loop from 100 to lets say 1000 or even more. I think loops an algorithms in tests can be used if they are aware of expectations and still valid, if they can be extended. Special scenarios like days in a month have no values > 31, so there is no reason, to extend a loop for days of a month to more than 31. Days in a month is commonly known thing. But other types of information can of course be mor exceptional, like 0 kelvin = -273.15 degrees Celsius and an algorithm testing a colder temperature would be some how out of scope. So when an algorithm is with out exceptions valid and easily predictable, testing of three values should be fine. Cheers, Andreas -- Posted via http://www.ruby-forum.com/. From mmazur at gmail.com Tue Mar 27 14:10:17 2012 From: mmazur at gmail.com (Mike Mazur) Date: Tue, 27 Mar 2012 22:10:17 +0800 Subject: [rspec-users] Why stop using mock_model in scaffold controller spec? Message-ID: Hi, In May last year, the controller specs generated with `rspec g scaffold` were changed to use real model objects instead of `mock_model`[1]. I'm curious why this change was made? Searching the rspec-users mailing list archives didn't turn up anything, and rspec-devel archives I found finish in Februrary 2011[2]. Thanks, Mike [1]: https://github.com/rspec/rspec-rails/commit/daef552453e89616e2762e87c0a77dc9b08a2220 [2]: http://rubyforge.org/pipermail/rspec-devel/ From mmazur at gmail.com Tue Mar 27 14:24:28 2012 From: mmazur at gmail.com (Mike Mazur) Date: Tue, 27 Mar 2012 22:24:28 +0800 Subject: [rspec-users] Why stop using mock_model in scaffold controller spec? In-Reply-To: References: Message-ID: Hi, On Tue, Mar 27, 2012 at 22:10, Mike Mazur wrote: > In May last year, the controller specs generated with `rspec g > scaffold` were changed to use real model objects instead of > `mock_model`. > > I'm curious why this change was made? As soon I sent this email off, I noticed this text in the template: # Compared to earlier versions of this generator, there is very limited use of # stubs and message expectations in this spec. Stubs are only used when there # is no simpler way to get a handle on the object needed for the example. # Message expectations are only used when there is no simpler way to specify # that an instance is receiving a specific message. That explains things a bit, but I'm still curious! One might argue that the earlier approach (with `mock_model`) may be better for at least two reasons: - looser coupling between specs and models - faster spec execution (ie: don't have to create a real ActiveRecord instance) I imagine RSpec is treading the fine line between ideal "best practices" (ie: minimize coupling) and being more pragmatic and easier to understand by the average developer. What's the current thinking on this topic? Thanks, Mike From weimar1927 at gmail.com Wed Mar 28 22:11:00 2012 From: weimar1927 at gmail.com (Hillary Hueter) Date: Wed, 28 Mar 2012 15:11:00 -0700 (PDT) Subject: [rspec-users] validating if a cell has any of the options. Message-ID: <943003.858.1332972660870.JavaMail.geo-discussion-forums@vbuc18> I'm testing the filter on a table. One of the filter options is "Show All". So for my other tests I've been looping through the rows and seeing if the table cell that contains the status doesn't include text (cell.should_not == 'Active'). However when the filter is set to all it can include any of the three statuses (Active, Inactive, Deleted). Using any of the rspec matchers how can I validate this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dchelimsky at gmail.com Thu Mar 29 11:39:35 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 29 Mar 2012 06:39:35 -0500 Subject: [rspec-users] Why stop using mock_model in scaffold controller spec? In-Reply-To: References: Message-ID: On Tue, Mar 27, 2012 at 9:24 AM, Mike Mazur wrote: > Hi, > > On Tue, Mar 27, 2012 at 22:10, Mike Mazur wrote: >> In May last year, the controller specs generated with `rspec g >> scaffold` were changed to use real model objects instead of >> `mock_model`. >> >> I'm curious why this change was made? > > As soon I sent this email off, I noticed this text in the template: > > # Compared to earlier versions of this generator, there is very limited use of > # stubs and message expectations in this spec. ?Stubs are only used when there > # is no simpler way to get a handle on the object needed for the example. > # Message expectations are only used when there is no simpler way to specify > # that an instance is receiving a specific message. > > That explains things a bit, but I'm still curious! > > One might argue that the earlier approach (with `mock_model`) may be > better for at least two reasons: > > - looser coupling between specs and models > - faster spec execution (ie: don't have to create a real ActiveRecord instance) > > I imagine RSpec is treading the fine line between ideal "best > practices (ie: minimize coupling) and being more pragmatic and easier > to understand by the average developer. What's the current thinking on > this topic? It wasn't really a loose coupling with models, it was a loose coupling with the underlying data. It was still very tightly coupled to ActiveRecord APIs (all, find, etc) because the generated controller code is coupled to those APIs. In rails-2, you could stub Model.find, and if you added constraints to find the specs would still pass because you were still using find. In rails-3, additional constraints are added via ARel APIs, which don't end up going through find, so you have to change all the specs when you change the implementation. This created additional friction which was annoying to people who understood what was going on, and confusing for people who didn't. In terms of the speed benefit, I do think that has merit and I stub model APIs all the time, but they are domain-specific APIs, not ActiveRecord APIs (most of the time). e.g. rather than stubbing Article.where("date >= ?", Date.today - 1.week) I'll stub Article.recent and call that from the controller. This, however, is not something that a generator can do for you since it doesn't know your domain. HTH, David From dchelimsky at gmail.com Thu Mar 29 11:45:21 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 29 Mar 2012 06:45:21 -0500 Subject: [rspec-users] validating if a cell has any of the options. In-Reply-To: <943003.858.1332972660870.JavaMail.geo-discussion-forums@vbuc18> References: <943003.858.1332972660870.JavaMail.geo-discussion-forums@vbuc18> Message-ID: On Wed, Mar 28, 2012 at 5:11 PM, Hillary Hueter wrote: > I'm testing the filter on a table. One of the filter options is "Show All". > So for my other tests I've been looping through the rows and seeing if the > table cell that contains the status doesn't include text (cell.should_not == > 'Active'). Please post one of these examples so we can see precisely what you're already doing. Thx, David > However when the filter is set to all it can include any of the three > statuses (Active, Inactive, Deleted). > > Using any of the rspec matchers how can I validate this? From dchelimsky at gmail.com Thu Mar 29 11:55:57 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Thu, 29 Mar 2012 06:55:57 -0500 Subject: [rspec-users] [rspec-rails] Mailer tests should be similar to Controller tests In-Reply-To: <9862337.609.1332807633683.JavaMail.geo-discussion-forums@vbbfw10> References: <9862337.609.1332807633683.JavaMail.geo-discussion-forums@vbbfw10> Message-ID: On Mon, Mar 26, 2012 at 7:20 PM, Paulo Luis Franchini Casaretto wrote: > Hey, > > When I'm testing a controller, I basically test three things. > Does it assign the variables the view needs? > Does it render the right template? > Does it do whatever the action is supposed to do? > And when testing mailers I feel it should be basically the same. > Does it assign the variables the view needs? > Does it render the right template? > Does it set the right headers? > I don't want to test the actual mail object, the same way I don't want to > test the views in controller tests. > On the other hand, the mail object IS the output of the mailer, so I don't > know. > What do you guys think about this? Seems reasonable, but unlikely to change. Here's why. rspec-rails provides wrappers around test classes provided by rails. Rails functional tests support the three questions you pose above, but rails mailer tests are different. From http://guides.rubyonrails.org/action_mailer_basics.html: "Testing mailers normally involves two things: One is that the mail was queued, and the other one that the email is correct." To support what you'd like to see in mailer specs, rspec-rails would have to provide it's own ExampleGroup (rather than wrap the rails class), which would have to be tightly bound to rails' internals. I took great pains in rspec-rails-2 to constrain coupling to public APIs, and this has had a big payoff: we've only had one case where a rails 3.x release required a release of rspec-rails (i.e. there was a breaking change). With rails-2, pretty much every release broke rspec-rails because rspec-rails was tied to internals (rspec-rails' fault, not rails). If you really want to see this change, you'll need to get it changed in rails itself, at which point rspec-rails will happily wrap the new and improved MailerTestCase. HTH, David From weimar1927 at gmail.com Fri Mar 30 16:52:58 2012 From: weimar1927 at gmail.com (Hillary Hueter) Date: Fri, 30 Mar 2012 09:52:58 -0700 (PDT) Subject: [rspec-users] validating if a cell has any of the options. In-Reply-To: References: <943003.858.1332972660870.JavaMail.geo-discussion-forums@vbuc18> Message-ID: <4759914.2981.1333126378713.JavaMail.geo-discussion-forums@vbdn7> The example as it exists today: it "should show all applications" do I.new do |c| c.login_flow(:userid => $support, :password => $password) ## Logs in c.manage_application_page.filter.when_present.flash c.manage_application_page.filter.when_present.select_value("All") ## Checks that the dropdown exists and changes it to "Show All Applications" #Assertion c.manage_application_page.applications_table.tr.each do | cell | if ["Active", "Inactive", "Deleted"].include? cell.text() puts cell.text() end end end end An example of the other tests: it "should show all active applications" do ### Checking the Application Filter "Show All Active Applications" is working properly Insight.new do |c| c.login_flow(:userid => $support, :password => $password) ## Logs in c.manage_application_page.filter.when_present.flash c.manage_application_page.filter.when_present.select_value("AllActive") ## Checks that the dropdown exists and changes it to "Show All Active Applications" #Assertion c.manage_application_page.applications_table.tr.each do |cell| cell.text.should_not == 'Deleted' cell.text.should_not == 'Inactive' end end end On Thursday, March 29, 2012 4:45:21 AM UTC-7, dchel... at gmail.com wrote: > > On Wed, Mar 28, 2012 at 5:11 PM, Hillary Hueter > wrote: > > I'm testing the filter on a table. One of the filter options is "Show > All". > > So for my other tests I've been looping through the rows and seeing if > the > > table cell that contains the status doesn't include text > (cell.should_not == > > 'Active'). > > Please post one of these examples so we can see precisely what you're > already doing. > > Thx, > David > > > However when the filter is set to all it can include any of the three > > statuses (Active, Inactive, Deleted). > > > > Using any of the rspec matchers how can I validate this? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jko170 at gmail.com Sat Mar 31 00:30:28 2012 From: jko170 at gmail.com (Justin Ko) Date: Fri, 30 Mar 2012 18:30:28 -0600 Subject: [rspec-users] validating if a cell has any of the options. In-Reply-To: <4759914.2981.1333126378713.JavaMail.geo-discussion-forums@vbdn7> References: <943003.858.1332972660870.JavaMail.geo-discussion-forums@vbuc18> <4759914.2981.1333126378713.JavaMail.geo-discussion-forums@vbdn7> Message-ID: <5D1F243B-B51F-43E1-B8CE-8E011BB258C9@gmail.com> On Mar 30, 2012, at 10:52 AM, Hillary Hueter wrote: > > The example as it exists today: > it "should show all applications" do > I.new do |c| > c.login_flow(:userid => $support, :password => $password) ## Logs in > c.manage_application_page.filter.when_present.flash > c.manage_application_page.filter.when_present.select_value("All") ## Checks that the dropdown exists and changes it to "Show All Applications" > #Assertion > c.manage_application_page.applications_table.tr.each do | cell | > if ["Active", "Inactive", "Deleted"].include? cell.text() > puts cell.text() > end > end > end > end > > An example of the other tests: > > it "should show all active applications" do ### Checking the Application Filter "Show All Active Applications" is working properly > Insight.new do |c| > c.login_flow(:userid => $support, :password => $password) ## Logs in > c.manage_application_page.filter.when_present.flash > c.manage_application_page.filter.when_present.select_value("AllActive") ## Checks that the dropdown exists and changes it to "Show All Active Applications" > #Assertion > c.manage_application_page.applications_table.tr.each do |cell| > cell.text.should_not == 'Deleted' > cell.text.should_not == 'Inactive' > end > end > end > > > > > On Thursday, March 29, 2012 4:45:21 AM UTC-7, dchel... at gmail.com wrote: > On Wed, Mar 28, 2012 at 5:11 PM, Hillary Hueter wrote: > > I'm testing the filter on a table. One of the filter options is "Show All". > > So for my other tests I've been looping through the rows and seeing if the > > table cell that contains the status doesn't include text (cell.should_not == > > 'Active'). > Please post one of these examples so we can see precisely what you're > already doing. > > Thx, > David > > > However when the filter is set to all it can include any of the three > > statuses (Active, Inactive, Deleted). > > > > Using any of the rspec matchers how can I validate this? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users If you're trying to verify that a string can be *any* of some things, I would go with something like this: "foo".should match Regexp.union('Active', 'Inactive', 'Deleted') That is the same as: "foo".should match /Active|Inactive|Deleted/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at ruby-forum.com Sat Mar 31 05:11:45 2012 From: lists at ruby-forum.com (Fearless Fool) Date: Sat, 31 Mar 2012 07:11:45 +0200 Subject: [rspec-users] isolating controller tests from models Message-ID: <8ea0eacb72c553e0d1af4de20a0c0f54@ruby-forum.com> This is a rehash of a question I posed at: http://stackoverflow.com/questions/9952317/isolating-controller-tests-from-models The basic question: If I have a FoosController and a Foo model, can I run FoosController rspec tests without creating the foos database table? I haven't found a good way to avoid it. Consider this simple controller: # file: app/controllers/foos_controller.rb class FoosController < ApplicationController respond_to :json def create @foo = Foo.create(params[:foo]) respond_with @foo end end In my RSpec tests, I can do @foo = mock_model("Foo") Foo.stub(:create) { @foo } post :create, :format => :json ... but that fails in the call to respond_with @foo, because ActionController calls @foo.has_errors? which in turn hits the database to look up column names. I could stub out has_errors?, but that means that I'm reaching into the internals of the system -- probably not a good idea for a test suite. If the answer to my question "can I do controller testing without creating the underlying database tables?" is 'no', then I'll accept that (with regret). But I'd rather hear that it is somehow possible. - ff -- Posted via http://www.ruby-forum.com/. From jko170 at gmail.com Sat Mar 31 05:49:03 2012 From: jko170 at gmail.com (Justin Ko) Date: Fri, 30 Mar 2012 23:49:03 -0600 Subject: [rspec-users] isolating controller tests from models In-Reply-To: <8ea0eacb72c553e0d1af4de20a0c0f54@ruby-forum.com> References: <8ea0eacb72c553e0d1af4de20a0c0f54@ruby-forum.com> Message-ID: <69083A1F-86E1-474A-AE66-2DB7832FE4D4@gmail.com> On Mar 30, 2012, at 11:11 PM, Fearless Fool wrote: > This is a rehash of a question I posed at: > http://stackoverflow.com/questions/9952317/isolating-controller-tests-from-models > The basic question: If I have a FoosController and a Foo model, can I > run FoosController rspec tests without creating the foos database table? > I haven't found a good way to avoid it. > > Consider this simple controller: > > # file: app/controllers/foos_controller.rb > class FoosController < ApplicationController > respond_to :json > def create > @foo = Foo.create(params[:foo]) > respond_with @foo > end > end > > In my RSpec tests, I can do > > @foo = mock_model("Foo") > Foo.stub(:create) { @foo } > post :create, :format => :json > > ... but that fails in the call to respond_with @foo, because > ActionController calls @foo.has_errors? which in turn hits the database > to look up column names. I could stub out has_errors?, but that means > that I'm reaching into the internals of the system -- probably not a > good idea for a test suite. Oh, the irony! You're using rspec-rails, which reaches *much further* into the "system" than `has_errors?`. Mucking with a non-public API is not a "never ever" thing :) > > If the answer to my question "can I do controller testing without > creating the underlying database tables?" is 'no', then I'll accept that > (with regret). But I'd rather hear that it is somehow possible. > > - ff > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users From dchelimsky at gmail.com Sat Mar 31 11:50:27 2012 From: dchelimsky at gmail.com (David Chelimsky) Date: Sat, 31 Mar 2012 08:50:27 -0300 Subject: [rspec-users] isolating controller tests from models In-Reply-To: <69083A1F-86E1-474A-AE66-2DB7832FE4D4@gmail.com> References: <8ea0eacb72c553e0d1af4de20a0c0f54@ruby-forum.com> <69083A1F-86E1-474A-AE66-2DB7832FE4D4@gmail.com> Message-ID: On Sat, Mar 31, 2012 at 2:49 AM, Justin Ko wrote: > > On Mar 30, 2012, at 11:11 PM, Fearless Fool wrote: > >> This is a rehash of a question I posed at: >> http://stackoverflow.com/questions/9952317/isolating-controller-tests-from-models >> The basic question: If I have a FoosController and a Foo model, can I >> run FoosController rspec tests without creating the foos database table? >> I haven't found a good way to avoid it. >> >> Consider this simple controller: >> >> ? # file: app/controllers/foos_controller.rb >> ? class FoosController < ApplicationController >> ? ? respond_to :json >> ? ? def create >> ? ? ? @foo = Foo.create(params[:foo]) >> ? ? ? respond_with @foo >> ? ? end >> ? end >> >> In my RSpec tests, I can do >> >> ? @foo = mock_model("Foo") >> ? Foo.stub(:create) { @foo } >> ? post :create, :format => :json >> >> ... but that fails in the call to respond_with @foo, because >> ActionController calls @foo.has_errors? which in turn hits the database >> to look up column names. mock_model is not an ActiveRecord object, so if it's hitting the db then something's not wired up correctly. > I could stub out has_errors?, but that means >> that I'm reaching into the internals of the system -- probably not a >> good idea for a test suite. > > Oh, the irony! > > You're using rspec-rails, which reaches *much further* into the "system" than `has_errors?`. There's a difference between a library doing this and end-user specs doing this. They both bear risk, but the burden is on the library developer (and in one place) if it's the lib, whereas its on all end users who stray on their own. > Mucking with a non-public API is not a "never ever" thing :) Care to elaborate on what the risks are and when you think it's worth it? >> If the answer to my question "can I do controller testing without >> creating the underlying database tables?" is 'no', then I'll accept that >> (with regret). ?But I'd rather hear that it is somehow possible. Of course its possible - just a matter of whether its worth it. This relates to an earlier question which I believe you posted about why the generated controller specs changed for rails 3. respond_with is another addition to rails 3 that tightly couples controllers to models, making it more challenging to stub models in controller specs. If you're going to violate the "mock your code, not other people's" guideline, you might find it safer to do it at a higher level of abstraction: mock respond_with directly on the controller. Still a violation of stub/mock guidelines, but at least it deals with code that you can see and change in the controller. HTH, David From jko170 at gmail.com Sat Mar 31 23:38:13 2012 From: jko170 at gmail.com (Justin Ko) Date: Sat, 31 Mar 2012 17:38:13 -0600 Subject: [rspec-users] isolating controller tests from models In-Reply-To: References: <8ea0eacb72c553e0d1af4de20a0c0f54@ruby-forum.com> <69083A1F-86E1-474A-AE66-2DB7832FE4D4@gmail.com> Message-ID: <4A84AAA6-28EE-4FA8-97D3-3AFC2B1D1C88@gmail.com> Sent from my iPhone On Mar 31, 2012, at 5:50 AM, David Chelimsky wrote: > On Sat, Mar 31, 2012 at 2:49 AM, Justin Ko wrote: >> >> On Mar 30, 2012, at 11:11 PM, Fearless Fool wrote: >> >>> This is a rehash of a question I posed at: >>> http://stackoverflow.com/questions/9952317/isolating-controller-tests-from-models >>> The basic question: If I have a FoosController and a Foo model, can I >>> run FoosController rspec tests without creating the foos database table? >>> I haven't found a good way to avoid it. >>> >>> Consider this simple controller: >>> >>> # file: app/controllers/foos_controller.rb >>> class FoosController < ApplicationController >>> respond_to :json >>> def create >>> @foo = Foo.create(params[:foo]) >>> respond_with @foo >>> end >>> end >>> >>> In my RSpec tests, I can do >>> >>> @foo = mock_model("Foo") >>> Foo.stub(:create) { @foo } >>> post :create, :format => :json >>> >>> ... but that fails in the call to respond_with @foo, because >>> ActionController calls @foo.has_errors? which in turn hits the database >>> to look up column names. > > mock_model is not an ActiveRecord object, so if it's hitting the db > then something's not wired up correctly. > >> I could stub out has_errors?, but that means >>> that I'm reaching into the internals of the system -- probably not a >>> good idea for a test suite. >> >> Oh, the irony! >> >> You're using rspec-rails, which reaches *much further* into the "system" than `has_errors?`. > > There's a difference between a library doing this and end-user specs > doing this. They both bear risk, but the burden is on the library > developer (and in one place) if it's the lib, whereas its on all end > users who stray on their own. > >> Mucking with a non-public API is not a "never ever" thing :) > > Care to elaborate on what the risks are and when you think it's worth it? The risk is you might be changing the behaviour of rails, which means you're not testing the true behaviour of the system. That goes with the public API too, but you're less likely to screw that up, and is less brittle. In this case, if there is no way around mocking 'has_errors?', I think it's worth the benefit of not hitting the database. I wouldn't go past mocking more than one very simple internal method, that's just asking for trouble. It's all about trade offs, right? > >>> If the answer to my question "can I do controller testing without >>> creating the underlying database tables?" is 'no', then I'll accept that >>> (with regret). But I'd rather hear that it is somehow possible. > > Of course its possible - just a matter of whether its worth it. This > relates to an earlier question which I believe you posted about why > the generated controller specs changed for rails 3. respond_with is > another addition to rails 3 that tightly couples controllers to > models, making it more challenging to stub models in controller specs. > If you're going to violate the "mock your code, not other people's" > guideline, you might find it safer to do it at a higher level of > abstraction: mock respond_with directly on the controller. Still a > violation of stub/mock guidelines, but at least it deals with code > that you can see and change in the controller. OP, since you basically need to load the rails env for controller specs, I dont see a problem with hitting the DB to check columns. Usually, the downside to hitting the DB is performance, but as long as you're not querying/creating/deleting records there's no downside to it IMO. > > HTH, > David > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users