im trying to setup some unit tests with Elixir but running into this error below. What am I doing wrong?
cannot invoke remote function PropertyManager.Database.get/0 inside match
Here is my code:
property_manager_test.exs
defmodule PropertyManagerTest do
use ExUnit.Case
test "the truth" do
assert 1 + 1 == 2
end
test "get value from db" do
assert PropertyManager.Database.get() = "test this"
end
end
database.ex
defmodule PropertyManager.Database do
def get do
"test this"
end
end
Try with == instead of =
What you're doing in your code is a pattern match, which means it will try to match the right side to the pattern on the left side. A pattern can't contain function calls, which is probably the cause of your error.
Related
I am using GoLang RegEx to find a specific number in a message
Invite code for the server ABC
your code is: 4361858022791184384
I am using this RegEx
([0-9]){19}
I want to delete any message which does not contain any invite code.
So that people can only send invite code to that specific place and specific action can be performed. And useless messages get deleted automatically.
I tried to negate it, but it also ignores other numbers.
I want a regex that captures every message which does not contain exactly 19 digits number.
FindString returns an empty string on failure, and Find returns nil. So
you could test against that:
package main
import "regexp"
const s = `Invite code for the server ABC
your code is: 4361858022791184384`
func main() {
re := regexp.MustCompile(`\d{19}`)
find := re.FindString(s)
if find == "" {
panic(re)
}
println(find)
}
https://golang.org/pkg/regexp#Regexp.Find
https://golang.org/pkg/regexp#Regexp.FindString
I am trying to test a method with PhpUnit and Mockery. In the process of specifying a method should be called with arguments my test fails.
TEST:
$this->eventRepo = \Mockery::mock('Path\To\EventRepository');
$start = Carbon::createFromFormat('Ymd-H-i-s', '20141211-09-21-00');
$end = Carbon::createFromFormat('Ymd-H-i-s', '20141211-09-19-00');
$this->eventRepo
->shouldReceive('findEvent')
->withArgs([
$start,
$end,
'1',
'1234567891'
])
->andReturn($callEvent);
REAL CODE:
$start = Carbon::createFromFormat('Ymd-H-i-s', '20141211-09-20-00');
$end = Carbon::createFromFormat('Ymd-H-i-s', '20141211-09-20-00');
$event = $this->eventRepo->findEvent(
$start->subSeconds(60),
$end->addSeconds(60),
$id,
$number
);
ERROR FROM TEST:
Mockery\Exception\NoMatchingExpectationException: No matching handler found for EventRepo::findEvent(object(Carbon\Carbon), object(Carbon\Carbon), "1", "1234567891"). Either the method was unexpected or its arguments matched no expected argument list for this method
$this->eventRepo is a mocked in the test. The real code runs correctly. After the error displays, it, I guess var_dump()'s a instance of Carbon.
I have no idea what could be causing this. I tried googling it but not knowing what to google made that pretty worthless. Has anyone run into this before?
When using an object in with() or withArgs(), phpunit performs an === check. This means that it'll look for the exact same instance of the class, not just any instance of Carbon.
In this case, it means that findEvent() is receiving an instance of Carbon, but not the exact same instance that you have in the actual code.
I have the following two Spock tests:
def "sends a valid response when no users exist"() {
setup:
def exchange = new HttpServerExchange(Mock(ServerConnection))
usersRepository.size() >> 0
when:
firstRunHandler.handleRequest(exchange)
then:
1*response.send(exchange, _)
}
def "does not send content when any users exist"() {
setup:
usersRepository.size() >> 1
when:
firstRunHandler.handleRequest(new HttpServerExchange(Mock(ServerConnection)))
then:
0*response.send(_, _)
}
The second one should definitely fail, since the interaction is stll there. But it always passes. I can not even make it fail with:
then:
assert false
0*response.send(_, _)
IntelliJ Idea still shows it as "green". But when I change the "then" to
then:
assert false
the test fails, so it is definitely being run and executed as a spock test.
I don't get much info from Spock, and I did not find out anything when debugging. What am I missing? What can I do to diagnose this problem?
I'm looking for asserts in the style of Google's testing framework, where something like ASSERT_LT(a, b) will check that $a is less than $b, and if not, will print the values of $a and $b in the error message. The asserts I've found so far just stop execution without printing anything useful by default. Any pointers?
Bonus: I like to write assertion-heavy code (not just in the tests), so it would be nice if the assertions are quick to evaluate.
Context: I came across this when writing unittests using this code as a template.
Specs2 defines a number of matchers such as must be_<=. At first I thought these needed to integrated into a specification, but at the bottom of the matchers page it says that they are modular functionality that "you should be able to reuse in your own test
framework". As an example, "You can reuse [traits such as] org.specs2.matcher.MustMatchers to write anything like 1 must be_==(1) and
get a Result back".
ScalaTest has its own matchers, as well. For example, one must be < (7).
def assert_<[A <% Ordered[A]](a: => A, b: => A) {
assert(a < b, "Assertion failed: expected %s < %s" format (a, b))
}
scala> assert_<(2, 1)
java.lang.AssertionError: assertion failed: Assertion failed: expected 2 < 1
I have some single watir.rb scripts that use IE and are written in a standard watir way.
How do I create a test group that combines them? I want all of them be executed by executing the main script.
Is it possible to auto include single test files into a test group by subidr?
Is it possible to enumerate the files that should be included in the test group?
Can I cascade (include other watir test groups in a watir test group)?
Edit: After much searching and googling I could not find anything.
I will use this simple style for now:
passed = 0
failed = 0
result = system("ruby suite_one.rb") #execute the script and wait for it to finish.
if result #Record our results.
passed = passed + 1
else
failed = failed + 1
end
result = system("ruby suite_two.rb") #execute the script and wait for it to finish.
if result #Record our results.
passed = passed + 1
else
failed = failed + 1
end
puts "failed: "
puts failed
puts "passed: "
puts passed
Take a look how Watir unit tests are executed.