-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RSpec's mock framework does not work with steps when in before :each #6
Comments
Yes, rspec-steps treat's all before() blocks as before(:all). This is because the purpose of rspec-steps is to run a sequence of steps in the same environment. Environment variables are not erased and transactions are not rolled back between steps, so it doesn't make sense to do something before every example. They are effectively treated as one single story with a shared environment. I'm not sure if mocks will do what you want here. We developed rspec-steps is for integration testing, and in that case we want to be testing against real data in the database. Mocks would defeat the purpose of that. So we exclusively use factories when doing integration testing; mocks only get used in controller tests. However, a couple of suggestions to try:
steps "whatever", :type => :request do it "should do something" do it "should show the right result" do end |
Thanks for your help anyways, Eugene From: Logical Reality Design [reply@reply.github.com] Yes, rspec-steps treat's all before() blocks as before(:all). This is because the purpose of rspec-steps is to run a sequence of steps in the same environment - the main function of rspec-steps is to keep the environment alive as you go from example to example. Environment variables are not erased and transactions are not rolled back between steps, so it doesn't make sense to do something before every example. I'm haven't ever used mocks in this environment. We developed rspec-steps is for integration testing, and in that case we want to be testing against real data in the database. Mocks would defeat the purpose of that. So we exclusively use factories when doing integration testing; mocks only get used in controller tests. So I'm not certain mocks will ever do what you want here. However, a couple of suggestions to try:
steps "whatever", :type => :request do it "should do something" do it "should show the right result" do end Reply to this email directly or view it on GitHub: |
Using mocks with rspec-steps isn't, on the other hand, something we're not interested in. You are in fact correct: before blocks in rspec-steps are "promoted" to before :all (there's even a warning to this effect) because running code between steps is problematic. It's possible that the MockSpace could be likewise promoted to being checked and cleared in the before :all space - it's unlikely this has enough value to be worth implementing, though. But, maybe stubbing would do what you want? Session.stub!(:current_user => User.new(:name => "I'm a mock!")) Specifically: do you care that current user gets called, or are you just trying to remove a dependency on outside code. |
@egilburg I know this is an ancient issue, but we recently released a new major version that should be more compatible with RSpec, and I think should address this issue. If you haven't moved completely on, could you give the newest version a try? |
I have an example e.g.:
Throughout execution of the "do something" steps (which used to be a single long "it") I'm depending on the mock to have been done. But it doesn't work:
I tried rewriting the example as such:
The result, and error message, is the same. Now, if I simply ditch the before :each and do the following:
The above DOES work, but it's very unDRY as I have many "it" statements. Ideally I'd want my first version, as my mock in question is generic (mocks the logged in user) and I really want it to simply work for every section in every steps.
I tried googling and found this:
http://www.ruby-forum.com/topic/217974
It seems that mock() doesn't work in "before :all", and from what I can guess, "steps" somehow implicitly converts a "before :each" to a "before :all"
I don't really care if "before :each" would run again prior to each "it" block, or only once per "steps" (but correctly making the mockspace objects available throughout each "it" block of the steps) since my mock statement is idempotent (but some people might care, e.g. if they create real records inside a before each). But from my needs, I just want to run the mock statement once and know that the result mocks are working within each "it" statement of the "steps"
Is it possible to achieve?
Thanks
The text was updated successfully, but these errors were encountered: