Autospec / rspec not working, doing something wrong? - ruby

I wonder if this has its place on StackOverflow, but since it IS programming-related, I will shoot it away.
Here's my problem. I am new to TDD and I love Ruby, so the obvious path I'm taking is testing stuff with rspec. Why obvious? I saw it in diverse screencasts and thought it was really neat. Then I saw "autospec" somewhere, and tried to use it.
So I install the gem, using sudo gem install ZenTest (according to the instructions here)
Next, I go into my folder, containing "digit.rb" and "digit_spec.rb", and fire up autospec without any parameter. Nothing happens. Worthy of note that I have two tests in my spec file and that I can test it fine just using the spec command, but I'd be delighted to use autotest...
Any help/pointers/documentation link available? Please? :P

You need to create .autotest file containing this code:
Autotest.add_hook :reset do |at|
at.clear_mappings
at.add_mapping(/^(.*?)(_spec)?\.rb$/) { |filename, m|
if m[2]
filename
else
"#{m[1]}_spec.rb"
end
}
end
it changes the default mapping of file to spec

Maybe you can give spork + autospec a try. The spork instructions on the rspec wiki is probably the most current way to go: https://github.com/dchelimsky/rspec/wiki/spork---autospec-==-pure-bdd-joy-

Related

Update libraries after manual change

So I started working on my first open-source contribution in ruby. There I have the library I'm working on in the /lib/ folder. Now when I tried changing the code, my program (which uses the library) still uses the old code.
For example: Broke a function on purpose by deleting its end keyword (which should be causing an immediate crash), but it kept working perfectly after I did.
Another example was changing the code in such a way it should still work (mutating the output string) but it still returned the old string.
user~$ bin/ruby-hyphen -V "this is a test sentence"
this is a test sen-tence
Does anyone know if I have to tell the runtime to refresh it or something along those lines?
I found out why that happened. The file has a *.gemspec, which made it act as if it was a gem. To see the changes I needed to enter:
gem build *.gemspec
bundle exec rake install
Or, if you want to develop quicker: change everywhere you require it into a require_relative. That should also fix it. I hope this question helps someone in the future!

How to see the source code for an "update!' method

I have a web site that is killed due to a memory overflow. It is triggered during a PUT request coming from a users web browser. Unfortunately, the logs are not helpful in this case. I have traced the issue down to this method definition:
# app/controllers/registrations/profiles_controller.rb
def update
update! do |success, failure|
success.html { redirect_to edit_registration_diagnosis_path }
failure.html do
build_diagnosis
render 'edit'
end
end
end
I want to see the source code for this update! method. How do I ask ruby or rails or bash/grep to show me this source code?
I tried:
git grep 'def update!' # no results
My env:
$ rails --version
Rails 3.2.22.5
$ ruby --version
ruby 1.9.3p551
The libraries are not in the same directory as your Rails app, they'll be wherever your Ruby versions are, which differs depending on which version manager you used to install it.
The Rails docs are online at http://api.rubyonrails.org/, or you could use a gem like pry-byebug to step into the method during execution. As Ruby is object oriented and uses an inheritance chain to find an object that responds to a given message, this is the best way to really know which method is being called at any given point in the application's execution.
Add gem 'pry-byebug' to your gemfile, bundle install and then insert binding.pry at the top of your update method. Once execution pauses you can easily step into the method.
You can use byebug gem to see what is happening at each step in your method.
As others have said, the code in your app is not the full set of code that is running. So grep won't work here. You may also run into the issue where the same method is defined multiple times, and grep won't help there, either.
The best solution I've found is using pry. It looks like this is a Rails project, so you can get results most easily by adding the following to your Gemfile:
# Gemfile
gem "pry-rails"
gem "pry-doc"
At this point, the world is your oyster.
The easiest way to start learning how to use this is to add a binding.pry in your code execution where you want to explore. Then run the code in test or development environments, and your server will stop and give you a console at that line. Then you just show-source update! and you'll see where the method is defined.
So step 1, use pry and explore its many plugin libraries.
Step 2 is to try out solargraph in your text editor. It's not as powerful as pry, but it can help you jump to method definitions within your project easily. https://solargraph.org
Step 3 check out the premium text editor RubyMine, as it has support for this sort of thing and much more, though it's not free. https://www.jetbrains.com/help/ruby/getting-started.html

TestFirst.org Learn_ruby rake and depreciation warnings :should and :expect syntax

After many searches via Google I'm ready to get some input from the community. I'm trying to apply for App Academy in San Francisco and one of the required pre-work is TestFirst's Learn_ruby. I original had this configured on a Linux VM on my windows box. It worked very well. When I ran rake it would list one problem at a time; the text was is helpful colors; and the output was very condensed so I only got what I needed to read. I've recently wiped and configured my system as a dual boot Xubuntu 14.04/ Windows and the VM I once had is long gone. I worked to get Ruby setup in my linux vm just as I had done in the VM.
My problem now is when I run rake on my projects I get the same depreciation warning:
Deprecation Warnings:
Using should from rspec-expectations' old :should syntax without
explicitly enabling the syntax is deprecated. Use the new :expect
syntax or explicitly enable :should instead. Called from
/home/kaji/Projects/learn_ruby/05_silly_blocks/silly_blocks_spec.rb:25:in
`block (3 levels) in '.
After googling this back and forth I understand for the most part what the error is telling me. And I've even found a solution to get it to go away. Thanks to this post: RSpec's New Expectation Syntax, I was able to find a syntax to make the warning go away. (basicly had to change blah.should == # to expect(blah()).to eq(#) inside the *_spec.rb file. This seems to make the warning happy. However I still see signs that I have another problem.
The output isn't as 'friendly' as it was when I was on my VM. It's not in color; it dumps all the errors at once. What I enjoyed the most about learn_ruby was it gave me one objective at a time with minimal output. Now i have to scroll up quite a ways just to see what my issues are.
I've tired removing RVM completely and all gems and reinstalling to see if I could resolve this. No dice. I'm such a Ruby Noobie i'm a little overwhelmed with all this. I'd like to just get back to learning the basics but this has been troubling me for over a week. Has anyone had this problem/fix the except
I've also tried removing the rpsec ~<=2.0 from the Rakefile. I'm using ruby -v 2.1.2, rails 4.1.1 and i have RSpec 3.0.0, 2.99.0, 2.0.0. I even tried finding a tutorial on changing the syntax but it was real confusing as it introduced lots of Ruby concepts I have no idea on. At that point I feel like I'm over my head and there is something simple I'm missing.
Hope I provided enough info for assistance.
I hope I'm answering the correct question, since you seem to have figured out the one in the title (i.e. the preferred syntax having changed from blah.should to expect(blah).to). It would help if you changed the title to reflect the actual question.
RSpec.configure do |config|
config.fail_fast = true
config.color_enabled = true
}
will make RSpec fail at first error, rather than after running all tests, and enable the colours, for that one file.
Or you can run rspec with rspec --fail-fast --color.
Alternately, you can put this in $HOME/.rspec so it always does it:
--fail-fast
--color

Documenting Ruby code results within source file

This is a really basic question. I've been watching Ruby Tapas and really like how Avdi can run his Ruby code and have it recorded directly in the source file. Googling "executing and documenting Ruby code" obviously returns a bunch of stuff about the RDocs for the language itself. Anyone know more specifically what I should look into?
You need to use the t9md / vim-ruby-xmpfilter. Look also the README section.
You need to have rcodetools gem mandatory.
After that go though the issue, I logged.

How can I auto compile my HAML files into HTML files in a tiny project that doesn't run on RoR?

I have only today started playing with compass and haml. While I am quite familiar with the way sass works and I get the idea of what compass is for sass and how to use it, I've hit a little bit of a road block when it comes to using haml efficiently.
Of course I am hoping that someone here already knows the answer to my problem and can give me a little jump start into haml.
Here is what I'd like to accomplish:
Auto compile my HAML files when I save them.
The project however is just a tiny static site (couple of pages) to build up a template set for a later integration into the ExpressionEngine CMS (a php based solution).
So keeping in mind that myself using HAML to simply speed up the initial "Design to HTML/CSS" process, what is a good way to auto compile my HAML files into HTML, basically something that gives me a haml watch command that I can run on my project?
Is there even something like this out there?
As for the platform I am running on, I've got a Mac running OS X 10.6.6.
Thanks for reading, any ideas, suggestions, help would be very much appreciated.
Thank you both #Jacob and #Jonathan, I ultimately ended up using neither of your approaches in favour of using middleman, hence the answer to my own question.
For those reading this topic having a similar question in mind, the reason I like middleman so much is that it effectively combines my entire workflow into 1 mini-server app.
Using mm-ini project_name and then mm-server in the directory I instantly have access to Compass, HAML and SASS all with the option of simply outputting it to plain html at any given time.
Here is more info about middleman : http://middlemanapp.com/
Staticmatic and Nanoc also do HAML but as far as I could find out they do not 'out of the box' support Compass (SASS) compilation, which for some might be an upside but for me wasn't.
Again, thanks for your answers, here is however the answer that I ultimately chose to follow.
If you have Ruby installed you could use the watchr gem.
With the help of a little nice script that I found here you can start a process that recognizes any changes to your haml file.
Beneath you can find my customized watchr.rb
def compile_haml
%x[haml index.haml index.html]
end
def do_growl(message)
growlnotify = `which growlnotify`.chomp
title = "Watchr Message"
passed = message.include?('0 failures, 0 errors')
image = passed ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
severity = passed ? "-1" : "1"
options = "-w -n Watchr --image '#{File.expand_path(image)}'"
options << " -m '#{message}' '#{title}' -p #{severity}"
system %(#{growlnotify} #{options} &)
end
do_growl "Watching folders and waiting for changes..."
watch(".*\.haml$") { |x|
compile_haml
do_growl "Compiled HAML!"
}
If you do not have growl installed just leave that part away
I've found StaticMatic to be really good for building static web sites in HAML.
Maybee a bit more manual that you'd like, but you could always install the fs-events gem and do something along the lines of
require 'rb-fsevent'
require "open3"
include Open3
fsevent = FSEvent.new
fsevent.watch Dir.pwd do |directories|
puts "Detected change inside: #{directories.inspect}"
popen3('haml',
'..parameters..',
'..parameters..') do |stdin, stdout, stderr|
stdout.read.split("\n").each do |line|
puts line
end
end
end
fsevent.run
using values in the directoriesobject to call the hamlexecutable on changed files.
Although you've apparently found what you were looking for I'll still post another approach because middleman might not be the perfect solution for everyone.
My approach uses Rake. I've written a simple rakefile including a 'watch' task that recompiles my sass (or compass) and haml files whenever a file changes. Plus it reloads the browser preview :) (I don't know if middleman can do that).
The rakefile is on github: https://gist.github.com/1635301/
Codekit is what I use, it's fantastic and handles SASS, Compass, HAML, as well as many other things.

Resources